class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<>(); int n = s.length(), m = words.length; if(n == 0 || m == 0) return res; int len = words[0].length(); if(n < m * len) return res; Map<String,Integer> map = new HashMap<>(); int sum = 0; for(String str : words) { if(!map.containsKey(str)) sum++; map.put(str,map.getOrDefault(str,0)+1); } int l = 0; while(l + m * len <= n) { Map<String,Integer> window = new HashMap<>(); int count = 0; int r = l; String str = s.substring(r,r+len); while(map.containsKey(str)) { window.put(str,window.getOrDefault(str,0)+1); if(window.get(str) == map.get(str)) count++; if(window.get(str) > map.get(str)) break; if(count == sum) { res.add(l); break; } r += len; str = s.substring(r,r+len); } l++; } return res; } }