-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_and_replace_pattern.py
38 lines (26 loc) · 1.23 KB
/
find_and_replace_pattern.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
35
36
37
38
'''
Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.
'''
class Solution:
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
def find(w):
l = []
d = defaultdict(int)
i = 0
for char in w:
if char in d:
l.append(d[char])
else:
i+=1
d[char] = i
l.append(d[char])
return l
res = []
patternword_pattern = find(pattern)
for word in words:
word_pattern = find(word)
if word_pattern == patternword_pattern:
res.append(word)
return res