-
Notifications
You must be signed in to change notification settings - Fork 1
/
_030_SubstringWithConcatenationOfAllWords.py
43 lines (36 loc) · 1.32 KB
/
_030_SubstringWithConcatenationOfAllWords.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
#-----------------------------------------------------------------------------
# Runtime: 56ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
class Solution:
def findSubstring(self, s: str, words: list):
if len(words) == 0:
return words
word_dic = {}
for word in words:
if word in word_dic:
word_dic[word] += 1
else:
word_dic[word] = 1
word_length = len(words[0])
result = []
for i in range(word_length):
left = i
temp_dic = {}
for j in range(i, len(s) - word_length + 1, word_length):
current_word = s[j:j + word_length]
if current_word not in word_dic:
left = j + word_length
temp_dic = {}
continue
if current_word in temp_dic:
temp_dic[current_word] += 1
else:
temp_dic[current_word] = 1
while temp_dic[current_word] > word_dic[current_word]:
temp_dic[s[left:left + word_length]] -= 1
left += word_length
if temp_dic == word_dic:
result.append(left)
return result