Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create: 290-Word-Pattern.py
  • Loading branch information
UdayGarg committed Oct 6, 2022
commit 8948d679740b91374aac93bed055cb18079f29cd
16 changes: 16 additions & 0 deletions python/290-Word-Pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
words = s.split(" ")
if len(pattern) != len(words):
return False
charToWord = {}
wordToChar = {}

for c, w in zip(pattern, words):
if c in charToWord and charToWord[c] != w:
return False
if w in wordToChar and wordToChar[w] != c:
return False
charToWord[c] = w
wordToChar[w] = c
return True