-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
28 lines (24 loc) · 921 Bytes
/
solution.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
class Solution(object):
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
res = []
word_length = len(pattern)
for word in words:
char_map = {}
char_reverse_map = {}
not_match = False
for i in xrange(word_length):
if not char_map.get(word[i]) and not char_reverse_map.get(pattern[i]):
char_map[word[i]] = pattern[i]
char_reverse_map[pattern[i]] = word[i]
elif char_map.get(word[i]) != pattern[i]:
# same as char_reverse_map.get(pattern[i]) != word[i]
not_match = True
break
if not not_match:
res.append(word)
return res