-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathSubstring with Concatenation of All Words.java
More file actions
64 lines (52 loc) · 2.24 KB
/
Substring with Concatenation of All Words.java
File metadata and controls
64 lines (52 loc) · 2.24 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SubstringConcatenation {
public static List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>();
if (s == null || s.length() == 0 || words == null || words.length == 0) {
return result;
}
int wordLength = words[0].length();
int totalWords = words.length;
int windowSize = wordLength * totalWords;
Map<String, Integer> wordCountMap = new HashMap<>();
for (String word : words) {
wordCountMap.put(word, wordCountMap.getOrDefault(word, 0) + 1);
}
for (int i = 0; i < wordLength; i++) {
int left = i;
int right = i;
Map<String, Integer> currentWordCountMap = new HashMap<>();
while (right + wordLength <= s.length()) {
String word = s.substring(right, right + wordLength);
right += wordLength;
if (wordCountMap.containsKey(word)) {
currentWordCountMap.put(word, currentWordCountMap.getOrDefault(word, 0) + 1);
while (currentWordCountMap.get(word) > wordCountMap.get(word)) {
String leftWord = s.substring(left, left + wordLength);
currentWordCountMap.put(leftWord, currentWordCountMap.get(leftWord) - 1);
left += wordLength;
}
if (right - left == windowSize) {
result.add(left);
String leftWord = s.substring(left, left + wordLength);
currentWordCountMap.put(leftWord, currentWordCountMap.get(leftWord) - 1);
left += wordLength;
}
} else {
currentWordCountMap.clear();
left = right;
}
}
}
return result;
}
public static void main(String[] args) {
String s = "barfoothefoobarman";
String[] words = {"foo", "bar"};
List<Integer> result = findSubstring(s, words);
System.out.println("Indices of substrings: " + result);
}
}