Skip to content

Commit 604fbce

Browse files
committed
890
1 parent 1c8e8d6 commit 604fbce

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

HT/890.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Find and Replace Pattern
2+
3+
#### Description
4+
5+
[link](https://leetcode.com/problems/find-and-replace-pattern/)
6+
7+
---
8+
9+
#### Solution
10+
11+
- See Code
12+
13+
---
14+
15+
#### Code
16+
17+
> Complexity T : O(n) M : O(n)
18+
19+
```python
20+
class Solution:
21+
def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
22+
res = []
23+
24+
def match(word, pattern):
25+
if len(word) != len(pattern):
26+
return False
27+
d = {}
28+
for w, p in zip(word, pattern):
29+
if w not in d:
30+
if p in d.values():
31+
return False
32+
d[w] = p
33+
else:
34+
if d[w] != p:
35+
return False
36+
return True
37+
38+
for w in words:
39+
if match(w, pattern):
40+
res.append(w)
41+
return res
42+
```

0 commit comments

Comments
 (0)