Question:
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
import java.util.ArrayList;
import java.util.Hashtable;
public class Solution {
public ArrayList<Integer> findSubstring(String S, String[] L) {
ArrayList<Integer> res = new ArrayList<Integer>();
Hashtable<String,Integer> ht = new Hashtable<String, Integer>();
for(String str: L){
if(ht.containsKey(str)){
ht.put(str,ht.get(str)+1);
}else {
ht.put(str, 1);
}
}
int wordlen = L[0].length();
int wordnumber = L.length;
int totallen = wordlen*wordnumber;
for(int i =0;i<=S.length()-totallen;i++){
Hashtable<String,Integer> temp = new Hashtable<>(ht);
int count = 0;
for(int j=i;j<=i+totallen-wordlen;j+=wordlen){
String sub = S.substring(j, j+wordlen);
if(temp.containsKey(sub)){
int times = temp.get(sub);
if (times==1) {
temp.remove(sub);
}else {
temp.put(sub, times-1);
}
count++;
}else {
break;
}
}
if(count==wordnumber){
res.add(i);
}
}
return res;
}
}