-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy path0290.py
More file actions
25 lines (22 loc) · 711 Bytes
/
0290.py
File metadata and controls
25 lines (22 loc) · 711 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
class Solution:
def wordPattern(self, pattern, str_):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
str_map1, str_map2 = {}, {}
str_list = str_.split(' ')
str_list_len = len(str_list)
p_list_len = len(pattern)
if str_list_len != p_list_len:
return False
for i in range(p_list_len):
if str_map1.get(pattern[i]) != str_map2.get(str_list[i]):
return False
str_map1[pattern[i]] = str_map2[str_list[i]] = i
return True
if __name__ == "__main__":
pattern = "abba"
str_ = "dog dog dog dog"
print(Solution().wordPattern(pattern, str_))