Skip to content

Commit afee3f9

Browse files
committed
leetcode.com 290. Word Pattern
문제 링크: https://leetcode.com/problems/word-pattern
1 parent 4835515 commit afee3f9

File tree

1 file changed

+25
-0
lines changed
  • leetcode.com 290. Word Pattern

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def wordPattern(self, pattern: str, s: str) -> bool:
3+
ss = s.split()
4+
5+
if len(pattern) != len(ss):
6+
return False
7+
8+
c2word = {}
9+
word2c = {}
10+
11+
for idx in range(len(pattern)):
12+
c = pattern[idx]
13+
word = ss[idx]
14+
15+
if c not in c2word.keys():
16+
c2word[c] = word
17+
elif c2word[c] != word:
18+
return False
19+
20+
if word not in word2c.keys():
21+
word2c[word] = c
22+
elif word2c[word] != c:
23+
return False
24+
25+
return True

0 commit comments

Comments
 (0)