Skip to content

Commit 215ed15

Browse files
authored
Merge pull request neetcode-gh#1240 from UdayGarg/290
Create: 290-Word-Pattern.py
2 parents e487919 + 8948d67 commit 215ed15

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

python/290-Word-Pattern.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def wordPattern(self, pattern: str, s: str) -> bool:
3+
words = s.split(" ")
4+
if len(pattern) != len(words):
5+
return False
6+
charToWord = {}
7+
wordToChar = {}
8+
9+
for c, w in zip(pattern, words):
10+
if c in charToWord and charToWord[c] != w:
11+
return False
12+
if w in wordToChar and wordToChar[w] != c:
13+
return False
14+
charToWord[c] = w
15+
wordToChar[w] = c
16+
return True

0 commit comments

Comments
 (0)