这道题是参加周赛以来头一次做出来的第四题,但是出奇的简单,不认为是一个困难难度的题目。
加密解密字符串
给你一个字符数组 keys ,由若干 互不相同 的字符组成。还有一个字符串数组 values ,内含若干长度为 2 的字符串。另给你一个字符串数组 dictionary ,包含解密后所有允许的原字符串。请你设计并实现一个支持加密及解密下标从 0 开始字符串的数据结构。
字符串 加密 按下述步骤进行:
- 对字符串中的每个字符
c,先从keys中找出满足keys[i] == c的下标i。 - 在字符串中,用
values[i]替换字符c。
字符串 解密 按下述步骤进行:
- 将字符串每相邻 2 个字符划分为一个子字符串,对于每个子字符串
s,找出满足values[i] == s的一个下标i。如果存在多个有效的i,从中选择 任意 一个。这意味着一个字符串解密可能得到多个解密字符串。 - 在字符串中,用
keys[i]替换s。
实现 Encrypter 类:
Encrypter(char[] keys, String[] values, String[] dictionary)用keys、values和dictionary初始化Encrypter类。String encrypt(String word1)按上述加密过程完成对word1的加密,并返回加密后的字符串。int decrypt(String word2)统计并返回可以由word2解密得到且出现在dictionary中的字符串数目。
示例:
输入:
["Encrypter", "encrypt", "decrypt"]
[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]
输出:
[null, "eizfeiam", 2]
解释:
Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);
encrypter.encrypt("abcd"); // 返回 "eizfeiam"。
// 'a' 映射为 "ei",'b' 映射为 "zf",'c' 映射为 "ei",'d' 映射为 "am"。
encrypter.decrypt("eizfeiam"); // return 2.
// "ei" 可以映射为 'a' 或 'c',"zf" 映射为 'b',"am" 映射为 'd'。
// 因此,解密后可以得到的字符串是 "abad","cbad","abcd" 和 "cbcd"。
// 其中 2 个字符串,"abad" 和 "abcd",在 dictionary 中出现,所以答案是 2 。
提示:
1 <= keys.length == values.length <= 26values[i].length == 21 <= dictionary.length <= 1001 <= dictionary[i].length <= 100- 所有
keys[i]和dictionary[i]互不相同 1 <= word1.length <= 20001 <= word2.length <= 200- 所有
word1[i]都出现在keys中 word2.length是偶数keys、values[i]、dictionary[i]、word1和word2只含小写英文字母- 至多调用
encrypt和decrypt总计200次
可能这道题的难度在于题目的理解。
说了这么一大堆,给了一个嵌套了三层的数组,看的眼花缭乱。
加密过程比较简单,按照描述去模拟就好了。解密的过程可以按照前缀树或者DFS来处理。想了一下,写起来比较烦,干错碰碰运气,把解密也用加密来写,竟然通过了。
from collections import defaultdict
class Encrypter:
def __init__(self, keys: List[str], values: List[str], dictionary: List[str]):
self.keys = keys
self.values = values
self.dict = dictionary
self.re_kv = dict()
self.re_vk = defaultdict(list) # 手动做两个倒排索引,方便查找
for i in range(len(self.keys)):
v = self.keys[i]
self.re_kv[v] = i
for i in range(len(self.values)):
self.re_vk[self.values[i]].append(i)
print(self.re_kv)
print(self.re_vk)
def encrypt(self, word1: str) -> str:
res= ""
for i in range(len(word1)):
x = self.re_kv[word1[i]]
res += self.values[x]
return res
def decrypt(self, word2: str) -> int:
tmp = []
for w in self.dict:
tmp.append(self.encrypt(w))
res = 0
for t in tmp:
if word2 == t:
res += 1
return res
# Your Encrypter object will be instantiated and called as such:
# obj = Encrypter(keys, values, dictionary)
# param_1 = obj.encrypt(word1)
# param_2 = obj.decrypt(word2)