@@ -50,13 +50,220 @@ The output order does not matter, returning [9,0] is fine too.
50
50
### ** Python3**
51
51
52
52
``` 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
54
81
```
55
82
56
83
### ** Java**
57
84
58
85
``` 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#**
59
212
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
+ }
60
267
```
61
268
62
269
### ** ...**
0 commit comments