forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0030.py
36 lines (32 loc) · 1.16 KB
/
0030.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
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
if not s or not words:
return []
ans = []
dict = collections.defaultdict(int)
for word in words:
dict[word] += 1
wordLen = len(words[0])
for i in range(wordLen):
index = i
count = 0
tempMap = collections.defaultdict(int)
for j in range(i, len(s) - wordLen + 1, wordLen):
str = s[j:j + wordLen]
if str in dict.keys():
tempMap[str] += 1
count += 1
while tempMap[str] > dict[str]:
tempMap[s[index:index + wordLen]] -= 1
count -= 1
index += wordLen
if count == len(words):
ans.append(index)
tempMap[s[index:index + wordLen]] -= 1
count -= 1
index += wordLen
else:
tempMap.clear()
count = 0
index = j + wordLen
return ans