0


【LeetCode】30.串联所有单词的子串

串联所有单词的子串

题目描述:

给定一个字符串

  1. s

** **和一个字符串数组

  1. words

  1. words

中所有字符串 长度相同

  1. s

** **中的 串联子串 是指一个包含

  1. words

中所有字符串以任意顺序排列连接起来的子串。

  • 例如,如果 words = ["ab","cd","ef"], 那么 "abcdef""abefcd""cdabef""cdefab""efabcd", 和 "efcdab" 都是串联子串。 "acdbef" 不是串联子串,因为他不是任何 words 排列的连接。

返回所有串联子串在

  1. s

** **中的开始索引。你可以以 任意顺序 返回答案。

示例 1:

  1. **输入:**s = "barfoothefoobarman", words = ["foo","bar"]
  2. **输出:**[0,9]**解释:**因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6
  3. 子串 "barfoo" 开始位置是 0。它是 words 中以 ["bar","foo"] 顺序排列的连接。
  4. 子串 "foobar" 开始位置是 9。它是 words 中以 ["foo","bar"] 顺序排列的连接。
  5. 输出顺序无关紧要。返回 [9,0] 也是可以的。

示例 2:

  1. **输入:**s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
  2. **输出:**[]**解释:**因为words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16
  3. s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
  4. 所以我们返回一个空数组。

示例 3:

  1. **输入:**s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
  2. **输出:**[6,9,12]
  3. **解释:**因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9
  4. 子串 "foobarthe" 开始位置是 6。它是 words 中以 ["foo","bar","the"] 顺序排列的连接。
  5. 子串 "barthefoo" 开始位置是 9。它是 words 中以 ["bar","the","foo"] 顺序排列的连接。
  6. 子串 "thefoobar" 开始位置是 12。它是 words 中以 ["the","foo","bar"] 顺序排列的连接。

思路分析:

滑动窗口法

什么是滑动窗口法?

  1. 滑动窗口,可以用来解决一些查找满足一定条件的连续区间的性质(长度等)的问题。由于区间连续,因此当区间发生变化时,可以通过旧有的计算结果对搜索空间进行剪枝,这样便减少了重复计算,降低了时间复杂度。往往类似于“请找到满足xx的最x的区间(子串、子数组)的xx”这类问题都可以使用该方法进行解决。

一般滑动窗口维护两个指针,左指针和右指针。

当窗口内的元素未达到题目条件时,右指针右移,探索未知的区间来满足条件
当窗口内的元素达到题目条件时,左指针右移,压缩区间,使窗口尽可能短得满足题目条件

滑动窗口常规模板:

  1. int slidingWindow(vector<int> nums) {
  2. int n = nums.size();
  3. int ans = 0;
  4. // 记录窗口内的元素及其个数,非必要
  5. map<int, int> um;
  6. // l:窗口左边界; r:窗口右边界
  7. int l = 0, r = 0;
  8. // r 指针负责探索新的区间,直到搜索到nums的某末尾
  9. while (r < n) {
  10. um[r]++;
  11. // 如果区间不满足条件,l指针右移,窗口收缩
  12. while(区间 [l, r] is Invalid) {
  13. um[l]--;
  14. l++;
  15. }
  16. // 此处处理结果, deal with(ans, 区间[l, r])
  17. res = max(ans, r - l + 1); // 或者res = min(ans, r - l + 1);
  18. // 右指针右移,继续搜索
  19. r++;
  20. }
  21. return ans;
  22. }

代码实现注解:

  1. class Solution {
  2. public List<Integer> findSubstring(String s, String[] words) {
  3. // 单词的数量
  4. int allCount = words.length;
  5. // 单个单词的长度
  6. int aloneLen = words[0].length();
  7. List<Integer> ret = new ArrayList<>();
  8. //当遍历长度超过整个字符串的长度推出循环
  9. for (int i = 0; i < aloneLen; i++) {
  10. //Map集合获取键值对,map表示滑动窗口中的单词频次和words中的单词频率之差,string收集在words中出现的单词,int收集出现频率
  11. Map<String, Integer> map = new HashMap<>();
  12. //遍历words中的word,对窗口里的每个单词计算差值
  13. for (String word : words) {
  14. map.put(word, map.getOrDefault(word, 0) + 1);
  15. }
  16. // 左边界,起始下标
  17. int left = i;
  18. // 右边界
  19. int right = i;
  20. // 当前匹配上的单词数量
  21. int curCount = 0;
  22. //开始滑动窗口
  23. while (right <= s.length() - aloneLen) {
  24. //code用来存放即将进入窗口的单词
  25. String code = s.substring(right, right + aloneLen);
  26. //用于计录该单词需要的次数
  27. Integer cnt = map.getOrDefault(code, 0);
  28. if (cnt > 0) {
  29. // 右边的单词滑进来,并减数量
  30. curCount++;
  31. map.put(code, cnt - 1);
  32. right += aloneLen;
  33. if (curCount == allCount) {
  34. ret.add(left);
  35. }
  36. } else {
  37. // 如果没有剩余且有消耗,左边的单词滑出去
  38. if (curCount > 0) {
  39. String preCode = s.substring(left, left + aloneLen);
  40. map.put(preCode, map.get(preCode) + 1);
  41. left += aloneLen;
  42. curCount--;
  43. } else {
  44. // 这里增长 aloneLen,是一个单词的长度
  45. left += aloneLen;
  46. right += aloneLen;
  47. }
  48. }
  49. }
  50. }
  51. return ret;
  52. }
  53. }

标签: leetcode 算法

本文转载自: https://blog.csdn.net/m0_74197695/article/details/139093409
版权归原作者 A仔不会笑 所有, 如有侵权,请联系我们删除。

“【LeetCode】30.串联所有单词的子串”的评论:

还没有评论