Problem: Substring with Concatenation of All Words

You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters.

You can return the answer in any order.

Example 1:

Input: s = "barfoothefoobarman", words = ["foo","bar"] Output: [0,9] Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively. The output order does not matter, returning [9,0] is fine too. Example 2:

Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"] Output: [] Example 3:

Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"] Output: [6,9,12]

Constraints:

1 <= s.length <= 104 1 <= words.length <= 5000 1 <= words[i].length <= 30 s and words[i] consist of lowercase English letters.

```class Solution { public List findSubstring(String s, String[] words) {

    HashMap<String, Integer> input = new HashMap<>();
    int ID = 1;
    HashMap<Integer, Integer> count = new HashMap<>();
    for(String word: words) {
        if(!input.containsKey(word))
            input.put(word, ID++);
        int id = input.get(word);
        count.put(id,count.getOrDefault(id,0)+1);

    }
    int len = s.length();
    int wordLen = words[0].length();
    int numWords = words.length;
    int windowLen = wordLen*numWords;
    int lastIndex = s.length()-windowLen;

    int curWordId[] = new int[len];
    String cur = " "+s.substring(0,wordLen-1);

    //Change to int array
    for(int i = 0; i< (len-wordLen+1); i++) {
        cur = cur.substring(1, cur.length())+s.charAt(i+wordLen-1);
        if(input.containsKey(cur)){
            curWordId[i] = input.get(cur);
        } else {
            curWordId[i] = -1;
        }
    }
    List<Integer> res = new ArrayList<>();

    //compare using int make it faster 30 times in each comparison
    for(int i = 0; i<= lastIndex; i++) {

        HashMap<Integer, Integer> winMap = new HashMap<>();
        for(int j = 0; j < windowLen && curWordId[i] != -1; j+=wordLen) {

            int candidate = curWordId[j+i];

            if(!count.containsKey(candidate))
                break;
            else{
                winMap.put(candidate, winMap.getOrDefault(candidate, 0)+1);
            }
            if(winMap.get(candidate) > count.get(candidate))
                break;


            if(j == (windowLen - wordLen) && winMap.size() == count.size()){
                res.add(i);

            }

        }
    }

    return res;
}

} ```