-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProblem_10_WordsConcatenation.java
38 lines (35 loc) · 1.46 KB
/
Problem_10_WordsConcatenation.java
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
package Sliding_Window;
// Problem Statement: Words Concatenation (hard)
// LeetCode Question: 30. Substring with Concatenation of All Words
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Problem_10_WordsConcatenation {
public List<Integer> findWordConcatenation(String str, String[] words) {
Map<String, Integer> wordFrequencyMap = new HashMap<>();
for(String word : words){
wordFrequencyMap.put(word, wordFrequencyMap.getOrDefault(word, 0) + 1);
}
List<Integer> resultIndices = new ArrayList<Integer>();
int wordsCount = words.length, wordLength = words[0].length();
for (int i = 0; i <= str.length() - wordsCount * wordLength; i++) {
Map<String, Integer> wordsSeen = new HashMap<>();
for (int j = 0; j < wordsCount; j++) {
int nextWordIndex = i + j * wordLength;
String word = str.substring(nextWordIndex, nextWordIndex + wordLength);
if (!wordFrequencyMap.containsKey(word)) {
break;
}
wordsSeen.put(word, wordsSeen.getOrDefault(word, 0) + 1);
if (wordsSeen.get(word) > wordFrequencyMap.getOrDefault(word, 0)) {
break;
}
if (j + 1 == wordsCount) {
resultIndices.add(i);
}
}
}
return resultIndices;
}
}