forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt-and-decrypt-strings.py
More file actions
36 lines (30 loc) · 919 Bytes
/
encrypt-and-decrypt-strings.py
File metadata and controls
36 lines (30 loc) · 919 Bytes
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
# Time: ctor: O(m + d), m is len(keys), d is sum(len(x) for x in dictionary)
# encrypt: O(n)
# decrypt: O(n)
# Space: O(m + d)
import collections
import itertools
# freq table
class Encrypter(object):
def __init__(self, keys, values, dictionary):
"""
:type keys: List[str]
:type values: List[str]
:type dictionary: List[str]
"""
self.__lookup = {k: v for k, v in itertools.izip(keys, values)}
self.__cnt = collections.Counter(self.encrypt(x) for x in dictionary)
def encrypt(self, word1):
"""
:type word1: str
:rtype: str
"""
if any(c not in self.__lookup for c in word1):
return ""
return "".join(self.__lookup[c] for c in word1)
def decrypt(self, word2):
"""
:type word2: str
:rtype: int
"""
return self.__cnt[word2]