-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
29 lines (27 loc) · 762 Bytes
/
main.py
File metadata and controls
29 lines (27 loc) · 762 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
from collections import defaultdict
class Solution(object):
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
def find(w): # function that calculates the numeric pattern
l = []
m = defaultdict(int)
i = 0
for c in w:
if c in m:
l.append(m[c])
else:
i += 1
m[c] = i
l.append(m[c])
return l
ans = []
pfind = find(pattern)
for w in words:
wfind = find(w)
if wfind == pfind:
ans.append(w)
return ans