-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find and Replace Pattern.py
48 lines (30 loc) · 1.1 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
39
40
41
42
43
44
45
46
47
48
#https://leetcode.com/contest/weekly-contest-98/problems/find-and-replace-pattern/
from collections import defaultdict
class Solution:
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
ans=[]
pattern_match=[]
num=0
mapping={}
for c in pattern:
if(c not in mapping):
num+=1
mapping[c]=num
pattern_match.append(mapping[c])
for w in words:
pattern_current=[]
num=0
mapping={}
for c in w:
if(c not in mapping):
num+=1
mapping[c]=num
pattern_current.append(mapping[c])
if(pattern_current==pattern_match):
ans.append(w)
return ans