-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstringWithConcatenationOfAllWords.java
More file actions
295 lines (214 loc) · 9.21 KB
/
SubstringWithConcatenationOfAllWords.java
File metadata and controls
295 lines (214 loc) · 9.21 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package Algorithms.SlidingWindow;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 16 August 2025
* @link 30. Substring with Concatenation of All Words <a href="https://leetcode.com/problems/substring-with-concatenation-of-all-words/">LeetCode link</a>
* @topics String, Hash Table, Sliding Window / Dynamic Sliding Window
* @see Algorithms.SlidingWindow.MinimumWindowSubstring
* NOTE:
* Here we need the "concatenated string" --- i.e., all the words in words[] array concatenated in any permutation order
*/
public class SubstringWithConcatenationOfAllWords {
public static void main(String[] args) {
String s = "barfoofoobarthefoobarman";
String[] words = {"bar","foo","the"};
System.out.println("findSubstring Working But TLE => " + findSubstringWorkingButTLE(s, words));
System.out.println("findSubstring => " + findSubstring(s, words));
System.out.println("findSubstring using AbstractList => " + findSubstringUsingAbstractList(s, words));
}
/**
* @TimeComplexity O(m * n^2)
* @SpaceComplexity O(n)
*/
public static List<Integer> findSubstringWorkingButTLE(String s, String[] words) {
List<Integer> result = new ArrayList<>();
if(s==null || words==null || s.isEmpty() || words.length==0) return result;
Map<String, Integer> wordFreq = Arrays.stream(words).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)) );
int m = s.length();
int n = words.length;
int wordLen = words[0].length();
int windowLen = wordLen*n;
for(int l=0; l<m-windowLen+1; l++) { // s.length() - last_concatenated_string_length // --- O(m) iterations
Map<String, Integer> seen = new HashMap<>(); // or window --- reset the seen map for each window
for(int r=l; r-l<windowLen; r+=wordLen) { // O(n) iterations
String word = s.substring(r, r+wordLen); // O(wordLen) copy
seen.merge(word, 1, Integer::sum);
if(seen.equals(wordFreq)) { // O(n) comparison
result.add(l);
}
}
}
return result;
}
/**
* @TimeComplexity O(m * wordLen)
* @SpaceComplexity O(n * wordLen)
*
* Here if we start checking all indexes from 0 to wordLen ---> i.e., concatenated string might start from 0 or 1 or 2 or 3 or ... or (wordLen-1)
* eventually we get the result
*/
public static List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>();
if (s == null || words == null || s.isEmpty() || words.length == 0) return result;
Map<String, Integer> wordFreq = new HashMap<>();
for (String word : words) {
wordFreq.merge(word, 1, Integer::sum);
}
int m = s.length();
int n = words.length;
int wordLen = words[0].length();
// Try all starting points modulo / single wordLen
for (int i = 0; i<wordLen; i++) {
Map<String, Integer> seen = new HashMap<>();
for (int l=i, r=i, wCount=0; r+wordLen<=m; r+=wordLen) {
String word = s.substring(r, r + wordLen);
if (wordFreq.containsKey(word)) {
seen.merge(word, 1, Integer::sum); // --- increase the sliding window
wCount++;
// Too many of this word → shrink from l
while (seen.get(word) > wordFreq.get(word)) {
String leftWord = s.substring(l, l + wordLen); // --- decrease the sliding window
seen.merge(leftWord, -1, Integer::sum);
wCount--;
l += wordLen;
}
// Window is exactly n words → valid index, add l-index and shrink one word from l
if (wCount == n) {
result.add(l);
// Move l by one word to the right side to continue
String leftWord = s.substring(l, l + wordLen); // --- decrease the sliding window
seen.merge(leftWord, -1, Integer::sum);
wCount--;
l += wordLen;
}
} else {
seen = new HashMap<>(); // or seen.clear(); --- reset window
wCount = 0;
l = r+wordLen; // cause in next iteration, r+=wordLen --- i.e., making l=r
}
}
}
return result;
}
public static List<Integer> findSubstringUsingAbstractList(String s, String[] words) {
return new AbstractList<Integer>() {
List<Integer> list;
@Override
public Integer get(int index) {
if(list == null) list = getResults(s, words);
return list.get(index);
}
@Override
public int size() {
if(list == null) list = getResults(s, words);
return list.size();
}
};
}
private static List<Integer> getResults(String s, String[] words) {
List<Integer> result = new ArrayList<>();
if (s == null || words == null || words.length == 0) return result;
int wordLen = words[0].length();
int wordCount = words.length;
int totalLen = words.length * wordLen;
if (s.length() < totalLen) return result;
Map<String, Integer> wordMap = new HashMap<>();
for(String word: words) {
wordMap.merge(word, 1, Integer::sum);
}
for (int i = 0; i < wordLen; i++) {
int left = i, right = i;
int count = 0;
Map<String,Integer> windowMap = new HashMap<>();
while (right + wordLen <= s.length()) {
String part = s.substring(right, right + wordLen);
right += wordLen;
if(!wordMap.containsKey(part)) {
windowMap = new HashMap<>(); // or windowMap.clear();
count = 0;
left = right;
continue;
}
windowMap.merge(part, 1, Integer::sum);
count++;
while (windowMap.get(part) > wordMap.get(part)) {
String leftWord = s.substring(left, left + wordLen);
windowMap.merge(leftWord, -1, Integer::sum);
count--;
left += wordLen;
}
if (count == words.length) result.add(left);
}
}
return result;
}
public static List<Integer> findSubstring2(String s, String[] words) {
int m=s.length();
int wordLen=words[0].length();
int n=words.length;
int windowLen = wordLen*n;
List<Integer> ans=new ArrayList<>();
Map<String,Integer> wordFreq=new HashMap<>();
for(String word:words){
wordFreq.merge(word, 1, Integer::sum);
}
for(int idx=0; idx<wordLen; idx++) {
Map<String,Integer> seen = new HashMap<>();
StringBuilder sb = new StringBuilder();
for(int l=idx,r=idx; r<m; r++) {
sb.append(s.charAt(r));
if(sb.length()==wordLen){
String word=sb.toString();
seen.merge(word, 1, Integer::sum);
sb.setLength(0);
}
if(r-l+1 == windowLen) {
if(wordFreq.equals(seen)) {
ans.add(l);
}
String remove=s.substring(l, l+wordLen);
if(seen.get(remove)>1) {
seen.merge(remove, -1, Integer::sum);
}
else {
seen.remove(remove);
}
l+=wordLen;
}
}
}
return ans;
}
public static List<Integer> findSubstringWorkingButTLE2(String s, String[] words) {
List<Integer> result = new java.util.ArrayList<>();
if(s==null || words==null || s.isEmpty() || words.length==0) return result;
Map<String, Integer> wordFreq = new HashMap<>();
for(String word : words) {
wordFreq.merge(word, 1, Integer::sum);
}
int m = s.length();
int n = words.length;
int wordLen = words[0].length();
int windowLen = wordLen*n;
for(int i=0; i<m-windowLen+1; i++) {
Map<String, Integer> seen = new HashMap<>(); // reset the seen map for each window
for(int j=i; j<i+windowLen; j+=wordLen) {
String word = s.substring(j, j+wordLen);
if (!wordFreq.containsKey(word)) {
break;
}
seen.merge(word, 1, Integer::sum);
if(seen.get(word) > wordFreq.get(word)) {
break;
}
if(j+wordLen == i+windowLen) {
result.add(i);
}
}
}
return result;
}
}