-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30-FindSubstring.py
More file actions
243 lines (227 loc) · 7.45 KB
/
30-FindSubstring.py
File metadata and controls
243 lines (227 loc) · 7.45 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Python3
from typing import List
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if not s or not words:
return []
all_len = sum(map(len, words))
n = len(s)
words = Counter(words) # 统计“可迭代序列”中每个元素的出现的次数
res = []
for i in range(0, n - all_len + 1):
tmp = s[i:i+all_len]
flag = True
for key in words:
if words[key] != tmp.count(key):# tmp.count从字符串中间计算,会出现单词截断的问题
flag = False
break
if flag:
res.append(i)
return res
# 遍历和比较都是线性的,所以时间复杂度:O(n^2)
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if not s or not words:
return []
one_word = len(words[0])
all_len = len(words) * one_word
n = len(s)
words = Counter(words)
res = []
for i in range(0, n - all_len + 1):
tmp = s[i:i+all_len]
c_tmp = []
for j in range(0, all_len, one_word): # 手动切分单词,避免中间截断
c_tmp.append(tmp[j:j+one_word])
if Counter(c_tmp) == words:
res.append(i)
return res
# 滑动窗口, 在 s 中维护着一个所有单词长度总和的队列, 时间复杂度:O(n)
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if not s or not words:
return []
one_word = len(words[0])
word_num = len(words)
n = len(s)
words = Counter(words)
res = []
for i in range(0, one_word): # 在 s 中的遍历起点选择为n mod one_word, 即[0, one_word)
cur_cnt = 0
left = i
right = i
cur_Counter = Counter()
while right + one_word <= n:
w = s[right:right + one_word]
right += one_word
cur_Counter[w] += 1
cur_cnt += 1
while cur_Counter[w] > words[w]:
left_w = s[left:left + one_word]
left += one_word
cur_Counter[left_w] -= 1
cur_cnt -= 1
if cur_cnt == word_num:
res.append(left)
return res
# 优化
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
if not s or not words:
return []
one_word = len(words[0])
word_num = len(words)
n = len(s)
if n < one_word * word_num:
return []
words = Counter(words)
res = []
for i in range(0, one_word):
cur_cnt = 0
left = i
right = i
cur_Counter = Counter()
while right + one_word <= n:
w = s[right:right + one_word]
right += one_word
if w not in words: # 子串要与 words 中的单词完全匹配,中间不能有其他字符
left = right
cur_Counter.clear()
cur_cnt = 0
else:
cur_Counter[w] += 1
cur_cnt += 1
while cur_Counter[w] > words[w]:
left_w = s[left:left+one_word]
left += one_word
cur_Counter[left_w] -= 1
cur_cnt -= 1
if cur_cnt == word_num:
res.append(left)
return res
# C++ 多起点滑动窗口
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
if (s.empty() || words.empty()) {
return {};
}
vector<int> res;
int n = s.size(), m = words.size(), d = words[0].size();
int len = d * m;
if (n < len) {
return {};
}
unordered_map<string, int> um;
for (string w : words) {
um[w]++;
}
vector<unordered_map<string, int>> vum(d); // 多起点初始化map,n%d = [0, d)
for (int i = 0; i < d && i + len <= n; i++) {
for (int j = i; j < i + len; j += d) {
string t = s.substr(j, d);
vum[i][t]++;
}
if (vum[i] == um) {
res.emplace_back(i);
}
}
// 滑动窗口
for (int i = d; i + len <= n; i++) {
int r = i % d;
string ta = s.substr(i - d, d), tb = s.substr(i + len - d, d);
if(--vum[r][ta] == 0) {
vum[r].erase(ta);
}
vum[r][tb]++;
if (vum[r] == um) {
res.emplace_back(i);
}
}
return res;
}
};
# C++ 暴力
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
if (s.empty() || words.empty()) {
return {}; // return vector<int>();
}
int n = words.size(), m = words[0].size(), j = 0;
if (s.size() < m * n) {
return {};
}
vector<int> res;
unordered_map<string, int> um, tmp_um;
for (auto word : words) {
um[word]++;
}
string str = "";
for (int i = 0; i + m * n <= s.size(); i++) {
for (j = i; j < i + m * n; j += m) {
str = s.substr(j, m);
if (um.find(str) == um.end()) {
break; // 子串要与 words 中的单词完全匹配,中间不能有其他字符
}
tmp_um[str]++;
}
if (j == i + m * n && tmp_um == um) {
res.push_back(i);
}
tmp_um.clear();
}
return res;
}
};
# C++ 优化
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
if (s.empty() || words.empty()) {
return {};
}
int n = s.size(), m = words.size(), d = words[0].size();
int len = d * m;
if (n < len) {
return {};
}
vector<int> res;
unordered_map<string, int> um, tmp_um;
for (auto w : words) {
um[w]++;
}
for (int i = 0; i < d; i++) {
int cnt = 0;
int left = i;
int right = i;
tmp_um.clear();
while (right + d <= n) {
string right_w = s.substr(right, d);
right += d;
if (um.find(right_w) == um.end()) { // 子串要与 words 中的单词完全匹配,中间不能有其他字符
left = right;
tmp_um.clear();
cnt = 0;
} else {
tmp_um[right_w]++;
cnt++;
while (tmp_um[right_w] > um[right_w]) {
string left_w = s.substr(left, d);
left += d;
tmp_um[left_w]--;
cnt--;
}
if (cnt == m) {
res.push_back(left);
}
}
}
}
return res;
}
};