2
public List<Integer> findSubstring(String s, String[] words) {4
HashMap<String, Integer> input = new HashMap<>();6
HashMap<Integer, Integer> count = new HashMap<>();7
for (String word : words) {8
if (!input.containsKey(word)) input.put(word, ID++);9
int id = input.get(word);10
count.put(id, count.getOrDefault(id, 0) + 1);13
int wordLen = words[0].length();14
int numWords = words.length;15
int windowLen = wordLen * numWords;16
int lastIndex = s.length() - windowLen;18
int curWordId[] = new int[len];19
String cur = " " + s.substring(0, wordLen - 1);21
// Change to int array22
for (int i = 0; i < (len - wordLen + 1); i++) {23
cur = cur.substring(1, cur.length()) + s.charAt(i + wordLen - 1);24
if (input.containsKey(cur)) {25
curWordId[i] = input.get(cur);30
List<Integer> res = new ArrayList<>();32
// compare using int make it faster 30 times in each comparison33
for (int i = 0; i <= lastIndex; i++) {35
HashMap<Integer, Integer> winMap = new HashMap<>();36
for (int j = 0; j < windowLen && curWordId[i] != -1; j += wordLen) {38
int candidate = curWordId[j + i];40
if (!count.containsKey(candidate)) break;42
winMap.put(candidate, winMap.getOrDefault(candidate, 0) + 1);44
if (winMap.get(candidate) > count.get(candidate)) break;46
if (j == (windowLen - wordLen) && winMap.size() == count.size()) {