2325.解密消息

给你字符串 key 和 message ,分别表示一个加密密钥和一段加密消息。解密 message 的步骤如下:
使用 key 中 26 个英文小写字母第一次出现的顺序作为替换表中的字母 顺序 。
将替换表与普通英文字母表对齐,形成对照表。
按照对照表 替换 message 中的每个字母。
空格 ‘ ‘ 保持不变。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/decode-the-message
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def decodeMessage(self, key, message):
"""
:type key: str
:type message: str
:rtype: str
"""
#统一存ASCII码,用chr()将ASCII转换为字母,空格符ASCII码是32
mapping_dict = dict({" ":32})
start_index = 97
for i in key:
if i not in mapping_dict:
mapping_dict[i] = start_index
start_index += 1
decode = str()
for i in message:
decode += chr(mapping_dict[i])
return decode