2325
给你字符串 key 和 message ,分别表示一个加密密钥和一段加密消息。解密 message 的步骤如下:
- 使用
key中 26 个英文小写字母第一次出现的顺序作为替换表中的字母 顺序 。 - 将替换表与普通英文字母表对齐,形成对照表。
- 按照对照表 替换
message中的每个字母。 - 空格
' '保持不变。
- 例如,
key = "happy boy"(实际的加密密钥会包含字母表中每个字母 至少一次),据此,可以得到部分对照表('h' -> 'a'、'a' -> 'b'、'p' -> 'c'、'y' -> 'd'、'b' -> 'e'、'o' -> 'f')。
返回解密后的消息。

输入:key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv" 输出:"this is a secret" 解释:对照表如上图所示。 提取 "the quick brown fox jumps over the lazy dog" 中每个字母的首次出现可以得到替换表。
from collections import OrderedDict class Solution(object): def decodeMessage(self, key, message): """ :type key: str :type message: str :rtype: str """ r='' key=key.replace(' ','') newkey=''.join(OrderedDict.fromkeys(key)) for char in message: if(char==' '): r=r+' ' else: r=r+chr(newkey.find(char)+97) return r

第一次用from collections import OrderedDict
Work Hard
But do not forget to enjoy life😀
本文来自博客园,作者:YuhangLiuCE,转载请注明原文链接:https://www.cnblogs.com/YuhangLiuCE/p/17084027.html
posted on 2023-02-01 20:03 YuhangLiuCE 阅读(196) 评论(0) 收藏 举报
浙公网安备 33010602011771号