https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/
解决方案
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new ArrayList<>();
if (s == null || s.length() == 0 || words == null || words.length == 0)
return res;
int one_word = words[0].length();
int word_num = words.length;
HashMap<String, Integer> map = new HashMap<>(word_num);
for (String word : words) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
HashMap<String, Integer> tmp_map = new HashMap<>(word_num);
for (int i = 0; i < one_word; i++,tmp_map.clear()) {
for (int j = i + one_word ; j <= s.length() ; j+=one_word) {
String word = s.substring(j -one_word, j);
tmp_map.put(word, tmp_map.getOrDefault(word, 0) + 1);
if (j/one_word > word_num - 1){
int e0 = j - word_num * one_word;
String s0= s.substring(e0 ,e0 + one_word);
if (map.equals(tmp_map)) res.add(e0);
tmp_map.put(s0,tmp_map.get(s0)-1);
tmp_map.remove(s0,0);
}
}
}
return res;
}
}