30. 串联所有单词的子串

122 篇文章 0 订阅
32 篇文章 0 订阅

原题链接:30. 串联所有单词的子串

 

solution:

        哈希表+计数

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> res;    //定义返回值
        unordered_map<string,int> map;
        for(auto &x : words) map[x]++;  //存储words出现的次数

        //n单词长度,m个单词,字符串长度size
        int m = words.size(),n = words[0].size(),size = s.size();
        for(int i = 0;i + m * n <= size;i++) {
            //子串中出现的单词数量不可能超过words中出现的次数
            unordered_map<string,int> tmp;
            int j;
            //枚举m个单词
            for(j = 0;j < m;j++) {
                string str = s.substr(i + j * n,n); //获取子串
                if(!map.count(str)) break;
                tmp[str]++;
                if(tmp[str] > map[str]) break;
            }
            //否则代表拼接成功
            if(j == m) res.push_back(i);               
        }
        return res;
    }
};

        滑动窗口:

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> res;    //定义返回值
        unordered_map<string,int> map;
        for(auto &x : words) map[x]++;

        int m = words.size(),n = words[0].size(),size = s.size();
        //遍历分组
        /*
        假设n = 4,m = 2,size = 16
        i x x x i x x x i x x x i x x x
        x i x x x i x x x i x x x i x x
        x x i x x x i x x x i x x x i x
        x x x i x x x i x x x i x x x i
        分成4组
        */
        for(int i = 0;i < n;i++) {
            unordered_map<string,int> tmp;
            int cnt = 0;    //维护窗口内串联的有效单词数量
            for(int j = i;j + n <= size;j += n) {
                if(j >= i + m * n) {    //滑动窗口,移除窗口内第一个单词
                    string word = s.substr(j - m * n, n);
                    tmp[word]--;
                    if(tmp[word] < map[word]) cnt--;
                }
                string word = s.substr(j, n);
                tmp[word]++;
                if(tmp[word] <= map[word]) cnt++;
                if(cnt == m) res.push_back(j - (m - 1) * n);                    
            }
        }
        return res;
    }
};

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值