[LeetCode]#1309. Decrypt String from Alphabet to Integer Mapping
Environment: Python 3.7
Key technique: chr
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".
Analysis:
- In while loop if i < s length and i+2=”#”, this is double digit.
- Else is single digit.
- use char to covert to letter as below.
Solution:
class Solution:
def freqAlphabets(self, s):
ans=""
i=0
step=len(s)
while i < step:
if i + 2 < step and s[i + 2] == '#':
ans += chr(int(s[i:i+2]) + 96)
i += 3
else:
ans += chr(int(s[i]) + 96)
i += 1
return ans
Submission:
Reference: