Skip to content

Commit 752450f

Browse files
committedJun 23, 2022
feat: add solutions to lc problem: No.0030
No.0030.Substring with Concatenation of All Words
1 parent 8628726 commit 752450f

File tree

7 files changed

+551
-54
lines changed

7 files changed

+551
-54
lines changed
 

‎solution/0000-0099/0030.Substring with Concatenation of All Words/README.md

+210-1
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,231 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:哈希表 + 滑动窗口**
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

5961
<!-- 这里可写当前语言的特殊实现逻辑 -->
6062

6163
```python
62-
64+
class Solution:
65+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
66+
cnt = Counter(words)
67+
sublen = len(words[0])
68+
n, m = len(s), len(words)
69+
ans = []
70+
for i in range(sublen):
71+
cnt1 = Counter()
72+
l = r = i
73+
t = 0
74+
while r + sublen <= n:
75+
w = s[r: r + sublen]
76+
r += sublen
77+
if w not in cnt:
78+
l = r
79+
cnt1.clear()
80+
t = 0
81+
continue
82+
cnt1[w] += 1
83+
t += 1
84+
while cnt1[w] > cnt[w]:
85+
remove = s[l: l + sublen]
86+
l += sublen
87+
cnt1[remove] -= 1
88+
t -= 1
89+
if m == t:
90+
ans.append(l)
91+
return ans
6392
```
6493

6594
### **Java**
6695

6796
<!-- 这里可写当前语言的特殊实现逻辑 -->
6897

6998
```java
99+
class Solution {
100+
public List<Integer> findSubstring(String s, String[] words) {
101+
Map<String, Integer> cnt = new HashMap<>();
102+
for (String w : words) {
103+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
104+
}
105+
int subLen = words[0].length();
106+
int n = s.length(), m = words.length;
107+
List<Integer> ans = new ArrayList<>();
108+
for (int i = 0; i < subLen; ++i) {
109+
Map<String, Integer> cnt1 = new HashMap<>();
110+
int l = i, r = i;
111+
int t = 0;
112+
while (r + subLen <= n) {
113+
String w = s.substring(r, r + subLen);
114+
r += subLen;
115+
if (!cnt.containsKey(w)) {
116+
l = r;
117+
cnt1.clear();
118+
t = 0;
119+
continue;
120+
}
121+
cnt1.put(w, cnt1.getOrDefault(w, 0) + 1);
122+
++t;
123+
while (cnt1.get(w) > cnt.get(w)) {
124+
String remove = s.substring(l, l + subLen);
125+
l += subLen;
126+
cnt1.put(remove, cnt1.get(remove) - 1);
127+
--t;
128+
}
129+
if (m == t) {
130+
ans.add(l);
131+
}
132+
}
133+
}
134+
return ans;
135+
}
136+
}
137+
```
138+
139+
### **C++**
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
vector<int> findSubstring(string s, vector<string>& words) {
145+
unordered_map<string, int> cnt;
146+
for (auto& w : words) cnt[w]++;
147+
int subLen = words[0].size();
148+
int n = s.size(), m = words.size();
149+
vector<int> ans;
150+
for (int i = 0; i < subLen; ++i)
151+
{
152+
unordered_map<string, int> cnt1;
153+
int l = i, r = i;
154+
int t = 0;
155+
while (r + subLen <= n)
156+
{
157+
string w = s.substr(r, subLen);
158+
r += subLen;
159+
if (!cnt.count(w))
160+
{
161+
l = r;
162+
t = 0;
163+
cnt1.clear();
164+
continue;
165+
}
166+
cnt1[w]++;
167+
t++;
168+
while (cnt1[w] > cnt[w])
169+
{
170+
string remove = s.substr(l, subLen);
171+
l += subLen;
172+
cnt1[remove]--;
173+
--t;
174+
}
175+
if (t == m) ans.push_back(l);
176+
}
177+
}
178+
return ans;
179+
}
180+
};
181+
```
182+
183+
### **Go**
184+
185+
```go
186+
func findSubstring(s string, words []string) []int {
187+
cnt := map[string]int{}
188+
for _, w := range words {
189+
cnt[w]++
190+
}
191+
subLen := len(words[0])
192+
n, m := len(s), len(words)
193+
var ans []int
194+
for i := 0; i < subLen; i++ {
195+
cnt1 := map[string]int{}
196+
l, r := i, i
197+
t := 0
198+
for r+subLen <= n {
199+
w := s[r : r+subLen]
200+
r += subLen
201+
if _, ok := cnt[w]; !ok {
202+
l = r
203+
t = 0
204+
cnt1 = map[string]int{}
205+
continue
206+
}
207+
cnt1[w]++
208+
t++
209+
for cnt1[w] > cnt[w] {
210+
remove := s[l : l+subLen]
211+
l += subLen
212+
cnt1[remove]--
213+
t--
214+
}
215+
if t == m {
216+
ans = append(ans, l)
217+
}
218+
}
219+
}
220+
return ans
221+
}
222+
```
223+
224+
### **C#**
70225

226+
```cs
227+
public class Solution {
228+
public IList<int> FindSubstring(string s, string[] words) {
229+
var wordsDict = new Dictionary<string, int>();
230+
foreach (var word in words)
231+
{
232+
if (!wordsDict.ContainsKey(word))
233+
{
234+
wordsDict.Add(word, 1);
235+
}
236+
else
237+
{
238+
++wordsDict[word];
239+
}
240+
}
241+
242+
var wordOfS = new string[s.Length];
243+
var wordLength = words[0].Length;
244+
var wordCount = words.Length;
245+
for (var i = 0; i <= s.Length - wordLength; ++i)
246+
{
247+
var substring = s.Substring(i, wordLength);
248+
if (wordsDict.ContainsKey(substring))
249+
{
250+
wordOfS[i] = substring;
251+
}
252+
}
253+
254+
var result = new List<int>();
255+
for (var i = 0; i <= s.Length - wordLength * wordCount; ++i)
256+
{
257+
var tempDict = new Dictionary<string, int>(wordsDict);
258+
var tempCount = 0;
259+
for (var j = i; j <= i + wordLength * (wordCount - 1); j += wordLength)
260+
{
261+
if (wordOfS[j] != null && tempDict[wordOfS[j]] > 0)
262+
{
263+
--tempDict[wordOfS[j]];
264+
++tempCount;
265+
}
266+
else
267+
{
268+
break;
269+
}
270+
}
271+
if (tempCount == wordCount)
272+
{
273+
result.Add(i);
274+
}
275+
}
276+
277+
return result;
278+
}
279+
}
71280
```
72281

73282
### **...**

‎solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md

+208-1
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,220 @@ The output order does not matter, returning [9,0] is fine too.
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
55+
cnt = Counter(words)
56+
sublen = len(words[0])
57+
n, m = len(s), len(words)
58+
ans = []
59+
for i in range(sublen):
60+
cnt1 = Counter()
61+
l = r = i
62+
t = 0
63+
while r + sublen <= n:
64+
w = s[r: r + sublen]
65+
r += sublen
66+
if w not in cnt:
67+
l = r
68+
cnt1.clear()
69+
t = 0
70+
continue
71+
cnt1[w] += 1
72+
t += 1
73+
while cnt1[w] > cnt[w]:
74+
remove = s[l: l + sublen]
75+
l += sublen
76+
cnt1[remove] -= 1
77+
t -= 1
78+
if m == t:
79+
ans.append(l)
80+
return ans
5481
```
5582

5683
### **Java**
5784

5885
```java
86+
class Solution {
87+
public List<Integer> findSubstring(String s, String[] words) {
88+
Map<String, Integer> cnt = new HashMap<>();
89+
for (String w : words) {
90+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
91+
}
92+
int subLen = words[0].length();
93+
int n = s.length(), m = words.length;
94+
List<Integer> ans = new ArrayList<>();
95+
for (int i = 0; i < subLen; ++i) {
96+
Map<String, Integer> cnt1 = new HashMap<>();
97+
int l = i, r = i;
98+
int t = 0;
99+
while (r + subLen <= n) {
100+
String w = s.substring(r, r + subLen);
101+
r += subLen;
102+
if (!cnt.containsKey(w)) {
103+
l = r;
104+
cnt1.clear();
105+
t = 0;
106+
continue;
107+
}
108+
cnt1.put(w, cnt1.getOrDefault(w, 0) + 1);
109+
++t;
110+
while (cnt1.get(w) > cnt.get(w)) {
111+
String remove = s.substring(l, l + subLen);
112+
l += subLen;
113+
cnt1.put(remove, cnt1.get(remove) - 1);
114+
--t;
115+
}
116+
if (m == t) {
117+
ans.add(l);
118+
}
119+
}
120+
}
121+
return ans;
122+
}
123+
}
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
vector<int> findSubstring(string s, vector<string>& words) {
132+
unordered_map<string, int> cnt;
133+
for (auto& w : words) cnt[w]++;
134+
int subLen = words[0].size();
135+
int n = s.size(), m = words.size();
136+
vector<int> ans;
137+
for (int i = 0; i < subLen; ++i)
138+
{
139+
unordered_map<string, int> cnt1;
140+
int l = i, r = i;
141+
int t = 0;
142+
while (r + subLen <= n)
143+
{
144+
string w = s.substr(r, subLen);
145+
r += subLen;
146+
if (!cnt.count(w))
147+
{
148+
l = r;
149+
t = 0;
150+
cnt1.clear();
151+
continue;
152+
}
153+
cnt1[w]++;
154+
t++;
155+
while (cnt1[w] > cnt[w])
156+
{
157+
string remove = s.substr(l, subLen);
158+
l += subLen;
159+
cnt1[remove]--;
160+
--t;
161+
}
162+
if (t == m) ans.push_back(l);
163+
}
164+
}
165+
return ans;
166+
}
167+
};
168+
```
169+
170+
### **Go**
171+
172+
```go
173+
func findSubstring(s string, words []string) []int {
174+
cnt := map[string]int{}
175+
for _, w := range words {
176+
cnt[w]++
177+
}
178+
subLen := len(words[0])
179+
n, m := len(s), len(words)
180+
var ans []int
181+
for i := 0; i < subLen; i++ {
182+
cnt1 := map[string]int{}
183+
l, r := i, i
184+
t := 0
185+
for r+subLen <= n {
186+
w := s[r : r+subLen]
187+
r += subLen
188+
if _, ok := cnt[w]; !ok {
189+
l = r
190+
t = 0
191+
cnt1 = map[string]int{}
192+
continue
193+
}
194+
cnt1[w]++
195+
t++
196+
for cnt1[w] > cnt[w] {
197+
remove := s[l : l+subLen]
198+
l += subLen
199+
cnt1[remove]--
200+
t--
201+
}
202+
if t == m {
203+
ans = append(ans, l)
204+
}
205+
}
206+
}
207+
return ans
208+
}
209+
```
210+
211+
### **C#**
59212

213+
```cs
214+
public class Solution {
215+
public IList<int> FindSubstring(string s, string[] words) {
216+
var wordsDict = new Dictionary<string, int>();
217+
foreach (var word in words)
218+
{
219+
if (!wordsDict.ContainsKey(word))
220+
{
221+
wordsDict.Add(word, 1);
222+
}
223+
else
224+
{
225+
++wordsDict[word];
226+
}
227+
}
228+
229+
var wordOfS = new string[s.Length];
230+
var wordLength = words[0].Length;
231+
var wordCount = words.Length;
232+
for (var i = 0; i <= s.Length - wordLength; ++i)
233+
{
234+
var substring = s.Substring(i, wordLength);
235+
if (wordsDict.ContainsKey(substring))
236+
{
237+
wordOfS[i] = substring;
238+
}
239+
}
240+
241+
var result = new List<int>();
242+
for (var i = 0; i <= s.Length - wordLength * wordCount; ++i)
243+
{
244+
var tempDict = new Dictionary<string, int>(wordsDict);
245+
var tempCount = 0;
246+
for (var j = i; j <= i + wordLength * (wordCount - 1); j += wordLength)
247+
{
248+
if (wordOfS[j] != null && tempDict[wordOfS[j]] > 0)
249+
{
250+
--tempDict[wordOfS[j]];
251+
++tempCount;
252+
}
253+
else
254+
{
255+
break;
256+
}
257+
}
258+
if (tempCount == wordCount)
259+
{
260+
result.Add(i);
261+
}
262+
}
263+
264+
return result;
265+
}
266+
}
60267
```
61268

62269
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
vector<int> findSubstring(string s, vector<string>& words) {
4+
unordered_map<string, int> cnt;
5+
for (auto& w : words) cnt[w]++;
6+
int subLen = words[0].size();
7+
int n = s.size(), m = words.size();
8+
vector<int> ans;
9+
for (int i = 0; i < subLen; ++i)
10+
{
11+
unordered_map<string, int> cnt1;
12+
int l = i, r = i;
13+
int t = 0;
14+
while (r + subLen <= n)
15+
{
16+
string w = s.substr(r, subLen);
17+
r += subLen;
18+
if (!cnt.count(w))
19+
{
20+
l = r;
21+
t = 0;
22+
cnt1.clear();
23+
continue;
24+
}
25+
cnt1[w]++;
26+
t++;
27+
while (cnt1[w] > cnt[w])
28+
{
29+
string remove = s.substr(l, subLen);
30+
l += subLen;
31+
cnt1[remove]--;
32+
--t;
33+
}
34+
if (t == m) ans.push_back(l);
35+
}
36+
}
37+
return ans;
38+
}
39+
};

‎solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System.Collections.Generic;
2-
31
public class Solution {
42
public IList<int> FindSubstring(string s, string[] words) {
5-
if (words.Length == 0) return new List<int>();
6-
73
var wordsDict = new Dictionary<string, int>();
84
foreach (var word in words)
95
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
func findSubstring(s string, words []string) []int {
2+
cnt := map[string]int{}
3+
for _, w := range words {
4+
cnt[w]++
5+
}
6+
subLen := len(words[0])
7+
n, m := len(s), len(words)
8+
var ans []int
9+
for i := 0; i < subLen; i++ {
10+
cnt1 := map[string]int{}
11+
l, r := i, i
12+
t := 0
13+
for r+subLen <= n {
14+
w := s[r : r+subLen]
15+
r += subLen
16+
if _, ok := cnt[w]; !ok {
17+
l = r
18+
t = 0
19+
cnt1 = map[string]int{}
20+
continue
21+
}
22+
cnt1[w]++
23+
t++
24+
for cnt1[w] > cnt[w] {
25+
remove := s[l : l+subLen]
26+
l += subLen
27+
cnt1[remove]--
28+
t--
29+
}
30+
if t == m {
31+
ans = append(ans, l)
32+
}
33+
}
34+
}
35+
return ans
36+
}
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,38 @@
11
class Solution {
22
public List<Integer> findSubstring(String s, String[] words) {
3-
4-
List<Integer> re = new ArrayList<>();
5-
6-
if(s == null || words == null || words.length == 0 || words[0] == null) {
7-
return re;
3+
Map<String, Integer> cnt = new HashMap<>();
4+
for (String w : words) {
5+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
86
}
9-
if(s.length() == 0 || words[0].length() == 0 || s.length() < words.length * words[0].length()) {
10-
return re;
11-
}
12-
// 用< 单词,出现次数 > 来存储 words 中的元素,方便查找
13-
HashMap<String,Integer> map = new HashMap();
14-
for (String string : words) {
15-
map.put(string, map.getOrDefault(string,0) + 1);
16-
}
17-
int len = words[0].length();
18-
int strLen = s.length();
19-
int lastStart = len * words.length - len;
20-
21-
for (int i = 0; i < len; i++) {
22-
for (int j = i; j <= strLen - len - lastStart; j += len) {
23-
String tempStr = s.substring(j, j + len);
24-
if(map.containsKey(tempStr)) {
25-
HashMap<String,Integer> searched = new HashMap<>();
26-
// 从后向前依次对比
27-
int tempIndex = j + lastStart;
28-
String matchedStr = s.substring(tempIndex, tempIndex + len);
29-
while (tempIndex >= j && map.containsKey(matchedStr)) {
30-
// 正确匹配到单词
31-
if(searched.getOrDefault(matchedStr,0) < map.get(matchedStr)) {
32-
searched.put(matchedStr, searched.getOrDefault(matchedStr,0) + 1);
33-
}
34-
else {
35-
break;
36-
}
37-
tempIndex -= len;
38-
if(tempIndex < j) {
39-
break;
40-
}
41-
matchedStr = s.substring(tempIndex, tempIndex + len);
42-
}
43-
// 完全匹配所以单词
44-
if(j > tempIndex) {
45-
re.add(j);
46-
}
47-
// 从tempIndex 到 tempIndex + len 这个单词不能正确匹配
48-
else {
49-
j = tempIndex;
50-
}
7+
int subLen = words[0].length();
8+
int n = s.length(), m = words.length;
9+
List<Integer> ans = new ArrayList<>();
10+
for (int i = 0; i < subLen; ++i) {
11+
Map<String, Integer> cnt1 = new HashMap<>();
12+
int l = i, r = i;
13+
int t = 0;
14+
while (r + subLen <= n) {
15+
String w = s.substring(r, r + subLen);
16+
r += subLen;
17+
if (!cnt.containsKey(w)) {
18+
l = r;
19+
cnt1.clear();
20+
t = 0;
21+
continue;
22+
}
23+
cnt1.put(w, cnt1.getOrDefault(w, 0) + 1);
24+
++t;
25+
while (cnt1.get(w) > cnt.get(w)) {
26+
String remove = s.substring(l, l + subLen);
27+
l += subLen;
28+
cnt1.put(remove, cnt1.get(remove) - 1);
29+
--t;
30+
}
31+
if (m == t) {
32+
ans.add(l);
5133
}
5234
}
5335
}
54-
return re;
36+
return ans;
5537
}
5638
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
3+
cnt = Counter(words)
4+
sublen = len(words[0])
5+
n, m = len(s), len(words)
6+
ans = []
7+
for i in range(sublen):
8+
cnt1 = Counter()
9+
l = r = i
10+
t = 0
11+
while r + sublen <= n:
12+
w = s[r: r + sublen]
13+
r += sublen
14+
if w not in cnt:
15+
l = r
16+
cnt1.clear()
17+
t = 0
18+
continue
19+
cnt1[w] += 1
20+
t += 1
21+
while cnt1[w] > cnt[w]:
22+
remove = s[l: l + sublen]
23+
l += sublen
24+
cnt1[remove] -= 1
25+
t -= 1
26+
if m == t:
27+
ans.append(l)
28+
return ans

0 commit comments

Comments
 (0)
Please sign in to comment.