当前位置:网站首页>30. 串联所有单词的子串
30. 串联所有单词的子串
2022-06-23 14:38:00 【anieoo】
原题链接: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;
}
};
边栏推荐
- Starting from 3, add paging function in the business system
- Une compréhension simple du tri rapide
- 从3开始,在业务系统中增加分页功能
- The well-known face search engine provokes public anger: just one photo will strip you of your pants in a few seconds
- Error 1079 when starting a service: the account of this service is different from that of other services running on the same process
- What do you mean by waiting for insurance records? Where should I go for filing?
- Mysql双主配置的详细步骤
- k8s--部署单机版MySQL,并持久化
- 腾讯云服务器发送邮件失败
- Gartner最新报告:低代码应用开发平台在国内的发展
猜你喜欢

How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup

一款自动生成单元测试的 IDEA 插件

5分钟快速上线Web应用和API(Vercel)

JSR303数据校验

2021-04-15

How can genetic testing help patients fight disease?

如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup

js中的push函数介绍

Uniswap 收购 NFT交易聚合器 Genie,NFT 交易市场将生变局?

js遍历数组(用forEach()方法)
随机推荐
golang--判断字符串是否相等
[opencv450] salt and pepper noise demo
KDD'22「阿里」推荐系统中的通用序列表征学习
2021-06-03
js的slice()和splice()
2021-05-22
Illustration of ONEFLOW's learning rate adjustment strategy
2021-06-07
How is it safe to open an account for futures? Which futures company has a relatively low handling fee for futures and is suitable for retail investors to open an account?
The principle of redis cache consistency deep analysis
How to use note taking software flowus and note for interval repetition? Based on formula template
一款自动生成单元测试的 IDEA 插件
腾讯云服务器发送邮件失败
大厂架构师:如何画一张大气的业务大图?
2021-05-08
List query sorting parameter processing
Raspberry PI installing the wiring pi
useState vs useRef 和 useReducer:相同点、不同点和用例
力扣解法汇总513-找树左下角的值
基因检测,如何帮助患者对抗疾病?