-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathword.py
More file actions
51 lines (40 loc) · 1.34 KB
/
word.py
File metadata and controls
51 lines (40 loc) · 1.34 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution(object):
def wordPattern(self, pattern, string):
"""
:type pattern: str
:type str: str
:rtype: bool
"""
# Find the type of pattern first
# Initially create a mark of the pattern in a dictionary
pattern = list(pattern)
p = {}
for i in range(len(pattern)):
if pattern[i] not in p:
p[pattern[i]] = []
p[pattern[i]].append(i)
print p
# Separate the string and check for the pattern match
string = string.split(" ")
print string
if not string or not pattern:
return False
prev = "str"
check = None
for k,v in p.items():
count = 0
check = None
for val in v:
if val < len(string):
if not check:
check = string[val]
if check == string[val]:
count += 1
if check == prev:
return False
prev = check
if count == len(v) and len(v) == string.count(check):
pass
else:
return False
return True