-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathword2.py
More file actions
27 lines (25 loc) · 786 Bytes
/
word2.py
File metadata and controls
27 lines (25 loc) · 786 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
class Solution(object):
def wordPattern(self, pattern, str):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
# Logic 1:
# We loose the positions of the characters in string.. so this wont work
#return len(set(pattern)) == len(set(str.split(" ")))
# Logic 2:
relation = {}
visited = set()
s = str.split(" ")
if len(s) != len(pattern):
return False
for i, j in zip(pattern, s):
if i not in relation and j not in visited:
relation[i] = j
visited.add(j)
if i in relation and relation[i] == j:
pass
else:
return False
return True