import java.util.*;
public class WordConcatenation {
public static List<Integer> findWordConcatenation(String str, String[] words) {
// TODO: Write your code here
int windowStart = 0, matched = 0, index = 0;
StringBuffer rsb = new StringBuffer();
StringBuffer lsb = new StringBuffer();
List<Integer> indices = new ArrayList<Integer>();
HashMap<String, Integer> map = new HashMap<>();
for(String word : words) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
for(int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
char right = str.charAt(windowEnd);
rsb.append(right);
if(map.containsKey(rsb.toString())) {
map.put(rsb.toString(), map.get(rsb.toString()) - 1);
if(map.get(rsb.toString()) == 0) {
matched++;
System.out.println("String Buffer : " + rsb.toString());
System.out.println("Matched: " + matched);
rsb.delete(0, rsb.length());
}
}
if(matched == map.size()){
index = windowStart;
indices.add(index);
}
while(matched == map.size()) {
char left = str.charAt(windowStart);
lsb.append(left);
if(map.containsKey(lsb.toString())) {
if(map.get(lsb.toString()) == 0){
matched--;
System.out.println("String Buffer : " + rsb.toString());
System.out.println("Matched: " + matched);
}
map.put(lsb.toString(), map.get(lsb.toString()) + 1);
lsb.delete(0, lsb.length());
}
windowStart++;
}
}
return indices;
}
public static void main(String[] args) {
List<Integer> result = WordConcatenation.findWordConcatenation("catfoxcat", new String[] { "cat", "fox" });
System.out.println(result);
result = WordConcatenation.findWordConcatenation("catcatfoxfox", new String[] { "cat", "fox" });
System.out.println(result);
}
}
I feel like Im missing a super simple step to make this solution work. Can someone please help me