[Leet Code] Decrypt String from Alphabet to Integer Mapping

Matthew Boyd
2 min readDec 8, 2020

LeetCode: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/

Problem:

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:

Input: s = "10#11#12"
Output: "jkab"
Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2"
Input: s = "1326#"
Output: "acz"

Solution:

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

--

--