-
Notifications
You must be signed in to change notification settings - Fork 0
/
290.py
28 lines (24 loc) · 804 Bytes
/
290.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Runtime: 20 ms, faster than 97.12% of Python online submissions for Word Pattern.
# Difficulty: Easy
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
words = str.split(' ')
if (len(pattern) != len(words)):
return False
matches = dict()
for i in range(len(pattern)):
if pattern[i] in matches and matches[pattern[i]] != words[i]:
return False
else:
matches[pattern[i]] = words[i]
vals = set()
for element in matches.values():
if element in vals:
return False
vals.add(element)
return True