-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_the_message.py
52 lines (42 loc) · 1.68 KB
/
decode_the_message.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
'''
You are given the strings key and message, which represent a cipher key and a secret
message, respectively. The steps to decode message are as follows:
Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
Align the substitution table with the regular English alphabet.
Each letter in message is then substituted using the table.
Spaces ' ' are transformed to themselves.
For example, given key = "happy boy" (actual key would have at least one instance of each letter
in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').
Return the decoded message.
'''
from string import ascii_lowercase as ascii
class Solution:
def decodeMessage(self, key: str, message: str) -> str:
d = dict()
c = 0
for i in range(len(key)):
if key[i] != ' ':
if key[i] not in d:
d[key[i]] = ascii[c]
c += 1
res = ''
for i in range(len(message)):
if message[i] == ' ':
res += ' '
else:
res += d[message[i]]
return res
-----------------------------------------------------------------------------------
class Solution:
def decodeMessage(self, key: str, message: str) -> str:
mapping = {' ': ' '}
i = 0
res = ''
letters = 'abcdefghijklmnopqrstuvwxyz'
for char in key:
if char not in mapping:
mapping[char] = letters[i]
i += 1
for char in message:
res += mapping[char]
return res