30. 串联所有单词的子串 (滑动窗口)

 

给定一个字符串 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] 由小写英文字母组成

 

from collections import defaultdict
class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        need_dict = defaultdict(int)
        for w in words:
            need_dict[w]+=1
        res = []
        step = len(words[0])
        for i in range(step):
            left = right = i
            valid = 0
            cnt_dict = defaultdict(int)
            while right < len(s):
                cur = s[right:right+step]
                if cur in need_dict:
                    cnt_dict[cur]+=1
                    if cnt_dict[cur] == need_dict[cur]:
                        valid+=1
                right+=step
                while right-left>=len(words)*step:
                    cur2 = s[left:left+step]
                    if valid == len(need_dict):
                        res.append(left)
                    if cur2 in need_dict and cnt_dict[cur2] == need_dict[cur2]:
                        valid-=1
                    cnt_dict[cur2]-=1
                    left+=step
        return res
            

 

 

 

 

 1 class Solution {
 2 public:
 3     vector<int> findSubstring(string s, vector<string>& words) {
 4         unordered_map<string,int> need_map;
 5         for(auto word: words){
 6             need_map[word]++;
 7         }
 8         int len = words[0].size();
 9         vector<int> res;
10         // 每次滑动长度为len, 所以要从0,1,,, len 都作为起始位置 滑动一次才能保证所有位置都被遍历到了
11         for (int i = 0; i < len;++i) {
12             // 滑动窗口模板
13             int left = i;
14             int right = i;
15             unordered_map<string,int> window_map;
16             int valid_cnt = 0;
17             while(right < s.size()) {
18                 string cur_word = s.substr(right, len);
19                 if(need_map.find(cur_word)!=need_map.end()) {
20                     window_map[cur_word]++;
21                     if(need_map[cur_word] == window_map[cur_word]) {
22                         valid_cnt++;
23                     }
24                 }
25                 right+=len; // 注意每次滑动的长度
26                 while(right-left>=words.size()*words[0].size()) {
27                     string cur_word = s.substr(left,len);
28                     if (valid_cnt==need_map.size()) {
29                         res.emplace_back(left);
30                     }
31                     if(need_map.find(cur_word)!=need_map.end()) {
32                         if(need_map[cur_word] == window_map[cur_word]) {
33                             valid_cnt--;
34                         }
35                         window_map[cur_word]--;
36                     }
37                     left+=len;// 注意每次滑动的长度
38                 }
39             }
40         }
41         return res;
42     }
43 };

 

posted @ 2021-05-23 16:54  乐乐章  阅读(46)  评论(0编辑  收藏  举报