Skip to content

Commit 852fdbe

Browse files
committed
Time: 32 ms (74.62%), Space: 16.6 MB (36.29%) - LeetHub
1 parent ed8b592 commit 852fdbe

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def wordPattern(self, pattern: str, s: str) -> bool:
3+
patternMap, wordMap = {}, {}
4+
words = s.split(" ")
5+
if len(words) != len(pattern):
6+
return False
7+
for char, word in zip(pattern, words):
8+
if char not in patternMap:
9+
if word in wordMap:
10+
return False
11+
else:
12+
patternMap[char] = word
13+
wordMap[word] = char
14+
else:
15+
if patternMap[char] != word:
16+
return False
17+
return True
18+
19+
20+
pattern = "abba"
21+
s = "dog cat cat dog"
22+
print(Solution().wordPattern(pattern, s))

0 commit comments

Comments
 (0)