-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path290-Word Pattern.py
More file actions
28 lines (28 loc) · 909 Bytes
/
290-Word Pattern.py
File metadata and controls
28 lines (28 loc) · 909 Bytes
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
from collections import defaultdict
class Solution(object):
def wordPattern(self, pattern, s):
"""
:type pattern: str
:type s: str
:rtype: bool
"""
s_split = s.split(" ")
mappings = defaultdict()
pattern_len = len(pattern)
if len(s_split) % pattern_len != 0:
return False
i = 0
while i < len(s_split):
j = 0
while j < pattern_len and i + j < len(s_split):
if pattern[j] in mappings.keys():
if s_split[i + j] != mappings[pattern[j]]:
return False
else:
if s_split[i+j] in mappings.values():
return False
else:
mappings[pattern[j]] = s_split[i+j]
j += 1
i += pattern_len
return True