当前位置:网站首页>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 系列:存储引擎
- Golang-- judge whether the strings are equal
- 微信小程序引导用户添加小程序动画页
- WebService interface publishing and calling
- 2021-06-03
- k8s--部署单机版MySQL,并持久化
- MySQL create and manage tables
- Analysis and solution of connection failure caused by MySQL using replicationconnection
- php 二维数组插入
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
猜你喜欢

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

2021-05-22

Pop() element in JS

Selenium Edge的IE模式
Redis缓存三大异常的处理方案梳理总结

K8s-- deploy stand-alone MySQL and persist it

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

k8s--部署单机版MySQL,并持久化

2021-06-03

The work and development steps that must be done in the early stage of the development of the source code of the live broadcasting room
随机推荐
AXI_ Round_ Robin_ Arbiter design - aw and W channels
狂奔的极兔,摔了一跤
SFOD:无源域适配升级优化,让检测模型更容易适应新数据(附论文下载)
力扣解法匯總513-找樹左下角的值
建議自查!MySQL驅動Bug引發的事務不回滾問題,也許你正面臨該風險!
MySQL 创建和管理表
golang 重要知识:sync.Cond 机制
【云驻共创】智能供应链计划:提升供应链决策水平,帮助企业运营降本增效
MySQL advanced statement I
js的slice()和splice()
2021-05-08
变压器只能转换交流电,那直流电怎么转换呢?
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
After nine years at the helm, the founding CEO of Allen Institute retired with honor! He predicted that Chinese AI would lead the world
2021-05-08
Pop() element in JS
Google &huggingface| zero sample language model structure with the strongest ability
Golang -- multiple processing scenarios for files
港股今年最大IPO来了,660亿身家,坐在矿山上的“大王”
golang--文件的多个处理场景