-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path291.py
More file actions
21 lines (16 loc) · 671 Bytes
/
Copy path291.py
File metadata and controls
21 lines (16 loc) · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s"""
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
dic = {}
s = s.split()
if len(pattern) != len(s):
return False
for i in range(len(s)):
if pattern[i] in dic.keys() and dic[pattern[i]] != s[i]:
return False
else:
dic[pattern[i]] = s[i]
if len(set(dic.keys())) != len(set(dic.values())):
return False
return True