-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30. Substring with Concatenation of All Words.py
54 lines (49 loc) · 1.57 KB
/
30. Substring with Concatenation of All Words.py
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
import re
class Solution(object):
def findSubstring(self, s, words):
"""
:type s: str
:type words: List[str]
:rtype: List[int]
"""
numOfword = len(words)
str = words[0]
l = len(str)
le = l * numOfword
word_dict = dict()
result = []
for i in range(0, numOfword):
if word_dict.has_key(words[i]):
word_dict[words[i]] = word_dict[words[i]] + 1
else:
word_dict[words[i]] = 1
for left in range(0,len(s) - le + 1):
word_use_dict = dict()
right = left + le
str = s[left:right]
index = []
flag = 0
for i in range(0,numOfword):
if word_use_dict.has_key(words[i]):
continue
word_use_dict[words[i]] = 1
pattern = re.compile(words[i])
num = 0
offset = 0
for j in range(0,numOfword):
match = pattern.match(str,offset)
if match:
num = num + 1
index.append(match.start() + left)
offset = offset + l
if num != word_dict[words[i]]:
flag = 1
break
if flag != 1:
index.sort()
result.append(index[0])
return result
if __name__ == "__main__":
solution = Solution()
res = solution.findSubstring("barfoothefoobarman", ["foo", "bar"])
print res