-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1309. Decrypt String from Alphabet to Integer Mapping.py
54 lines (48 loc) · 1.39 KB
/
1309. Decrypt String from Alphabet to Integer Mapping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution:
# Dictionary that maintains the encoding of each numeric sequence
encoding = {
'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'
}
def freqAlphabets(self, s: str) -> str:
input_list = list(s);
result = list();
# First decode all the chars with #
while input_list.count("#") > 0:
first_occurance = input_list.index("#")
decoded_letter = self.encoding[''.join(input_list[first_occurance - 2:first_occurance + 1])]
# Remove the digits against that
input_list.pop(first_occurance - 2)
input_list.pop(first_occurance - 2)
input_list[first_occurance - 2] = decoded_letter
# Then decode all the remaining characters
for i in range(len(input_list)) :
if input_list[i].isnumeric() :
input_list[i] = self.encoding[input_list[i]]
return ''.join(input_list)
s = Solution()
s.freqAlphabets("")