https://leetcode.cn/problems/substring-with-concatenation-of-all-words/description/?envType=study-plan-v2&envId=top-interview-150
给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。
s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。
例如,如果 words = ["ab","cd","ef"], 那么 "abcdef", "abefcd","cdabef", "cdefab","efabcd", 和 "efcdab" 都是串联子串。 "acdbef" 不是串联子串,因为他不是任何 words 排列的连接。
返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。
classSolution{publicList<Integer>findSubstring(String s,String[] words){int len =0;Map<String,AtomicInteger> wordMap =newHashMap<>();for(String word : words){
len += word.length();
wordMap.computeIfAbsent(word, it ->newAtomicInteger()).getAndIncrement();}List<Integer> res =newArrayList<>();if(s.length()< len){return res;}int wordLen = words[0].length();for(int i =0; i + len <= s.length(); i++){if(hit(s, i, len, wordLen, wordMap)){
res.add(i);}}return res;}privatebooleanhit(String s,int pos,int len,int wordLen,Map<String,AtomicInteger> wordMap){Map<String,AtomicInteger> map =newHashMap<>();for(int i =0; i < len;){
map.computeIfAbsent(
s.substring(pos+i, pos+i+wordLen),
it ->newAtomicInteger()).getAndIncrement();
i+= wordLen;}for(Map.Entry<String,AtomicInteger> entry : wordMap.entrySet()){AtomicInteger now = map.get(entry.getKey());if(null== now || now.get()< entry.getValue().get()){returnfalse;}}returntrue;}}