Skip to content

Commit 752450f

Browse files
committed
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
### **...**

0 commit comments

Comments
 (0)