-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathword3.py
More file actions
28 lines (24 loc) · 824 Bytes
/
word3.py
File metadata and controls
28 lines (24 loc) · 824 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
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
words = s.split(" ")
pattern_ref = {}
value_ref = set()
# build pattern for reference
for i in list(pattern):
if i not in pattern_ref:
pattern_ref[i] = ""
if len(words) != len(pattern):
return False
# check the words
for w in range(len(words)):
word = words[w]
if pattern_ref[pattern[w]] == "" and word in value_ref:
return False
elif pattern_ref[pattern[w]] == "":
pattern_ref[pattern[w]] = word
value_ref.add(word)
elif pattern_ref[pattern[w]] == word:
continue
else:
return False
return True