forked from Azureki/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc890.py
38 lines (31 loc) · 826 Bytes
/
lc890.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
class Solution:
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
ans=[]
for word in words:
d=dict() #
for i in range(len(word)):
if word[i] not in d:
d[word[i]]=pattern[i]
else:
if d[word[i]]!=pattern[i]:
break
else:
d=dict() #
for i in range(len(word)):
if pattern[i] not in d:
d[pattern[i]]=word[i]
else:
if d[pattern[i]]!=word[i]:
break
else:
ans.append(word)
return ans
words = ["abc","deq","mee","aqq","dkd","ccc"]
pattern = "abb"
s=Solution()
print(s.findAndReplacePattern(words,pattern))