We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7512ef3 commit 70e886bCopy full SHA for 70e886b
14. Questions/leetcode 290 - word pattern.py
@@ -0,0 +1,26 @@
1
+# word pattern | leetcode 290 | https://leetcode.com/problems/word-pattern/
2
+# create a vocabulary to match pattern and a seen hashset to record seen words
3
+
4
+class Solution:
5
+ def wordPattern(self, pattern: str, s: str) -> bool:
6
+ vocab = dict()
7
+ seens = dict()
8
+ sent = s.split(" ")
9
10
+ if len(sent) != len(pattern):
11
+ return False
12
13
+ for i in range(len(pattern)):
14
+ i_patt = pattern[i]
15
+ i_sent = sent[i]
16
17
+ if vocab.get(i_patt):
18
+ if vocab[i_patt] != i_sent:
19
20
+ else:
21
+ if seens.get(i_sent):
22
23
+ vocab[i_patt] = i_sent
24
+ seens[i_sent] = True
25
26
+ return True
0 commit comments