题目:https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words
给定一个字符串 s 和一些 长度相同 的单词 words 。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。
示例 1:
输入:s = “barfoothefoobarman”, words = [“foo”,”bar”]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 “barfoo” 和 “foobar” 。
输出的顺序不重要, [9,0] 也是有效答案。
示例 2:
输入:s = “wordgoodgoodgoodbestword”, words = [“word”,”good”,”best”,”word”]
输出:[]
示例 3:
输入:s = “barfoofoobarthefoobarman”, words = [“bar”,”foo”,”the”]
输出:[6,9,12]
提示:
1 <= s.length <= 104
s 由小写英文字母组成
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i] 由小写英文字母组成
思路:
全排列不显示,时间复杂度O(m!)
题目说的是单词的无序串联,既然无序,说明与单词的顺序无关,只与单词的数量有关
1. 双哈希表暴力相等(我的)
class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer> res = new ArrayList<Integer>();Map<String,Integer> map = new HashMap<String,Integer>();if (s == null || s.length() == 0 || words == null || words.length == 0)return res;int words_num=words.length;int one_word=words[0].length();int all_words_len=words_num*one_word;for(int i=0;i<words_num;i++){map.put(words[i], map.getOrDefault(words[i], 0) + 1);}for (int i = 0; i < s.length() - all_words_len + 1; i++) {String tmp = s.substring(i, i + all_words_len);Map<String, Integer> tmp_map = new HashMap<String,Integer>();for (int j = 0; j < all_words_len; j += one_word) {String w = tmp.substring(j, j + one_word);tmp_map.put(w, tmp_map.getOrDefault(w, 0) + 1);}if (map.equals(tmp_map))res.add(i);}return res;}}

比上面的稍微优化一点…
class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer> res = new ArrayList<Integer>();Map<String,Integer> map = new HashMap<String,Integer>();if (s == null || s.length() == 0 || words == null || words.length == 0)return res;int words_num=words.length;int one_word=words[0].length();int all_words_len=words_num*one_word;for(int i=0;i<words_num;i++){map.put(words[i], map.getOrDefault(words[i], 0) + 1);}for (int i = 0; i < s.length() - all_words_len + 1; i++) {String tmp = s.substring(i, i + all_words_len);Map<String, Integer> tmp_map = new HashMap<String,Integer>();int index = i;while(index < i + wordNum * wordLen){String curWord = s.substring(index, index + wordLen);if(!allWords.containsKey(curWord) || tmp_map.get(curWord) == allWords.get(curWord)){break;}tmp_map.put(curWord, tmp_map.getOrDefault(curWord, 0) + 1);index += wordLen;}if(index == i + wordNum * wordLen){res.add(i);}return res;}}
2. 多起点+移动步长定长
public class Solution {public List<Integer> findSubstring(String s, String[] words) {ArrayList<Integer> res = new ArrayList<>();// 设 words中所有单词的长度为 dint n = s.length();int d = words[0].length();int len = 0; //窗口长度HashMap<String, Integer> unorder_Map = new HashMap<>();for (String word : words) {len += word.length();unorder_Map.put(word, unorder_Map.getOrDefault(word, 0) + 1);}// init: 初始化长度为 d 的数组//相当于多起点Map<String, Integer>[] map_Array = new Map[d];for (int i = 0; i < d && i + len <= n; i++) {map_Array[i] = new HashMap<>();for (int j = i; j < i + len; j += d) {String w = s.substring(j, j + d);map_Array[i].put(w, map_Array[i].getOrDefault(w, 0) + 1);}if (map_Array[i].equals(unorder_Map))res.add(i);}// sliding window: 滑动窗口,每次移动 d 个位置for (int i = d; i + len <= n; i++) {int r = i % d;String wA = s.substring(i - d, i);String wB = s.substring(i + len - d, i + len);map_Array[r].put(wA, map_Array[r].getOrDefault(wA, 1) - 1);if (map_Array[r].get(wA) == 0)map_Array[r].remove(wA);map_Array[r].put(wB, map_Array[r].getOrDefault(wB, 0) + 1);if (map_Array[r].equals(unorder_Map))res.add(i);}return res;}}

