describe

Given a string s formed by digits (‘0’ – ‘9’) and ‘#’ . We want to map s to English lowercase characters as follows:

  • Characters (‘a’ to ‘i’) are represented by (‘1’ to ‘9’) respectively.
  • Characters (‘j’ to ‘z’) are represented by (’10#’ to ’26#’) respectively.

Return the string formed after mapping.

It’s guaranteed that a unique mapping will always exist.

Example 1:

Input: s = "10#11#12"
Output: "jkab"
Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
Copy the code

Example 2:

Input: s = "1326#"
Output: "acz"
Copy the code

Example 3:

Input: s = "25#"
Output: "y"
Copy the code

Example 4:

Input: s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"
Output: "abcdefghijklmnopqrstuvwxyz"
Copy the code

Note:

  • 1 <= s.length <= 1000
  • s[i] only contains digits letters (‘0’-‘9’) and ‘#’ letter.
  • s will be valid string such that mapping is always possible.

parsing

According to the meaning of the question, convert the string combining numbers and # into an alphabetic string. According to the meaning of the question, store the conversion rule in the dictionary object D first, then according to the characteristics of the input, traverse s from back to front, as long as it is not #, then directly convert d into a letter, if it is #, Together with the previous two numbers combined into a string of conversion, the final string is reversed, is the final result.

answer

class Solution(object): def freqAlphabets(self, s): """ :type s: str :rtype: str """ d = {'1': 'a', '10#': 'j', '11#': 'k', '12#': 'l', '13#': 'm', '14#': 'n', '15#': 'o', '16#': 'p', '17#': 'q', '18#': 'r', '19#': 's', '2': 'b', '20#': 't', '21#': 'u', '22#': 'v', '23#': 'w', '24#': 'x', '25#': 'y', '26#': 'z', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i'} res = "" i=len(s)-1 while i>=0: if s[i]! ='#': res += d[s[i]] i -= 1 else : res += d[s[i-2:i+1]] i -= 3 return res[::-1]Copy the code

The results

Runtime: 16 ms, Faster than 88.89% of Python online submissions for Decrypt String from Alphabet to Integer Mapping. Memory Usage: Submissions in Python online submissions for Decrypt String from Alphabet to Integer Mapping.Copy the code

Original link: leetcode.com/problems/de…

Your support is my biggest motivation