当前位置:网站首页>30. concatenate substrings of all words
30. concatenate substrings of all words
2022-06-23 15:20:00 【anieoo】
Original link :30. Concatenate the substrings of all words
solution:
Hashtable + Count
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res; // Define the return value
unordered_map<string,int> map;
for(auto &x : words) map[x]++; // Storage words Number of occurrences
//n Word length ,m Word , String length size
int m = words.size(),n = words[0].size(),size = s.size();
for(int i = 0;i + m * n <= size;i++) {
// The number of words in the substring cannot exceed words Is the number of times
unordered_map<string,int> tmp;
int j;
// enumeration m Word
for(j = 0;j < m;j++) {
string str = s.substr(i + j * n,n); // Get substring
if(!map.count(str)) break;
tmp[str]++;
if(tmp[str] > map[str]) break;
}
// Otherwise, the splicing is successful
if(j == m) res.push_back(i);
}
return res;
}
};The sliding window :
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> res; // Define the return value
unordered_map<string,int> map;
for(auto &x : words) map[x]++;
int m = words.size(),n = words[0].size(),size = s.size();
// Traversal group
/*
hypothesis 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
Divide into 4 Group
*/
for(int i = 0;i < n;i++) {
unordered_map<string,int> tmp;
int cnt = 0; // Maintain the number of valid words concatenated in the window
for(int j = i;j + n <= size;j += n) {
if(j >= i + m * n) { // The sliding window , Remove the first word in the window
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;
}
};
边栏推荐
猜你喜欢
mysql主从只同步部分库或表的思路与方法

信贷产品额度定价场景下的回归模型效果评估

巴比特 | 元宇宙每日必读:Meta、微软等科技巨头成立元宇宙标准论坛组织,华为、阿里加入,英伟达高管称欢迎来自加密世界的参与者...

2021-05-22

狂奔的极兔,摔了一跤

Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!

mysql 系列:存储引擎

Gartner最新报告:低代码应用开发平台在国内的发展

山东:美食“隐藏款”,消费“扫地僧”

2021-06-07
随机推荐
[opencv450] salt and pepper noise demo
JS的unshift()和shift()
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
Ie mode of selenium edge
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
golang 重要知识:mutex
重卡界销售和服务的“扛把子”,临沂广顺深耕产品全生命周期服务
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
变压器只能转换交流电,那直流电怎么转换呢?
[普通物理] 光的衍射
建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!
Gartner最新报告:低代码应用开发平台在国内的发展
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?
2021-05-08
JS里的数组
AXI_Round_Robin_Arbiter 设计 - AW、W通道部分
mysql 系列:总体架构概述
力扣解法匯總513-找樹左下角的值
快速排序的简单理解
Summary of operating system underlying knowledge (interview)