Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70e886b

Browse files
committed
added: word pattern
1 parent 7512ef3 commit 70e886b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
return False
20+
else:
21+
if seens.get(i_sent):
22+
return False
23+
vocab[i_patt] = i_sent
24+
seens[i_sent] = True
25+
26+
return True

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /