Skip to content

Commit eefb03e

Browse files
Add files via upload
1 parent f0cfc40 commit eefb03e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 360ms 66.91%
2+
class Solution:
3+
def findSubstring(self, s, words):
4+
"""
5+
:type s: str
6+
:type words: List[str]
7+
:rtype: List[int]
8+
"""
9+
if not s or not words:
10+
return []
11+
num_words = len(words)
12+
len_words = len(words[0])
13+
counter = {} # 当做一个计数器
14+
for word in words:
15+
if word in counter:
16+
counter[word] += 1
17+
else:
18+
counter[word] = 1
19+
start_index = []
20+
for i in range(len(s) - num_words * len_words + 1):
21+
temp_counter, used_words = {}, 0
22+
for j in range(i, i + (num_words - 1) * len_words + 1, len_words):
23+
word = s[j:j+len_words]
24+
if word in counter:
25+
if word in temp_counter:
26+
temp_counter[word] += 1
27+
if temp_counter[word] > counter[word]:
28+
break
29+
used_words += 1
30+
else:
31+
temp_counter[word] = 1
32+
used_words += 1
33+
else:
34+
break
35+
if used_words == num_words:
36+
start_index.append(i)
37+
return start_index

0 commit comments

Comments
 (0)