-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm890.py
34 lines (30 loc) · 951 Bytes
/
m890.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
29
30
31
32
33
34
class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
currVal = 0
hs = {}
output = []
for c in pattern :
if c in hs :
output.append(hs[c])
else :
output.append(currVal)
hs[c] = currVal
currVal += 1
patternTuple = tuple(output)
matches = []
for word in words :
if len(word) != len(pattern) :
continue
currVal = 0
hs = {}
output = []
for c in word :
if c in hs :
output.append(hs[c])
else :
output.append(currVal)
hs[c] = currVal
currVal += 1
if tuple(output) == patternTuple :
matches.append(word)
return matches