-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathle-30.py
More file actions
115 lines (94 loc) · 2.91 KB
/
le-30.py
File metadata and controls
115 lines (94 loc) · 2.91 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import time
class Solution2(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
result = []
word_size = len(words[0])
window_size = word_size * len(words)
hashmap = {}
for w in words:
if not hashmap.get(w):
hashmap[w] = 1
else:
hashmap[w] += 1
def split_by_fixed_count(s, count):
return [s[i:i+count] for i in range(0, len(s), count)]
i = 0
while i < (len(s) - window_size) + 1:
substr = s[i:i+window_size]
hashmap_copy = hashmap.copy()
substr_list = split_by_fixed_count(substr, word_size)
# Check if substr holds all words of the `words` list.
for w in substr_list:
value = hashmap_copy.get(w)
#print("idx:", idx)
if not value:
break
if value == 1:
hashmap_copy.pop(w) # Remove the key
else:
hashmap_copy[w] = value - 1
#print('after replace:', substr)
if not hashmap_copy: # If all keys cleared.
result.append(i)
i += 1
return result
class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
result = []
word_size = len(words[0])
window_size = word_size * len(words)
i = 0
while i < (len(s) - window_size) + 1:
substr = s[i:i+window_size]
#print(substr)
# Check if substr holds all words of the `words` list.
for w in words:
idx = self.find_word_index(substr, w, word_size)
#print("idx:", idx)
if idx < 0:
break
substr = substr[:idx] + substr[idx+word_size:]
#print('after replace:', substr)
if not substr:
result.append(i)
i += 1
return result
def find_word_index(self, string, word, word_size):
"""
Find index of the target word in the string.
"""
start = 0
while True:
idx = string.find(word, start)
if idx < 0 or idx % word_size == 0:
return idx
else:
start += 1
s = "barfoothefoobarman"
words = ["foo","bar"]
#s= "wordgoodgoodgoodbestword"
#words = ["word","good","best","word"]
#s = "ababaab"
#words = ["ab","ba","ba"]
"""
s = "barfoothefoobarman"
words = ["foo","bar"]
s= "wordgoodgoodgoodbestword"
words = ["word","good","best","word"]
s = "ababaab"
words = ["ab","ba","ba"]
"""
start = time.time()
solution = Solution2()
print(solution.findSubstring(s, words))
print('time consumed:', time.time() - start)