[Leetcode]_30 Substring with Concatenation of All Words
来源:互联网 发布:mac 键盘符号 编辑:程序博客网 时间:2024/06/15 05:06
/** * Index: 30 * Title: Substring with Concatenation of All Words * Author: ltree98 **/
题意就是求字符串s中是否有子串是全由容器words内字符串组成的。
模拟法
模拟求这个问题的方法:
1.取原串一部分,然后看子串容器中是否有它,如果有往下看(跳到2);如果没有则失败。(回到1)
2.子串容器中有这部分,看用了几次,用的次数小于等于子串容器中拥有的个数,继续往下(跳到3),否则失败。(回到1)
3.如果已经匹配完了所有子串,成功,记录序列值。(回到1)
这里要注意一下,构建比较标准映射的值,不可以全置为固定值,而是要用递增。
因为,子串可能有重复的值。
class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { int sLen = s.length(), wordsSize = words.size(), perWordLen = words[0].length(); map<string, int> baseMap; for(int i = 0; i < wordsSize; i++) ++baseMap[words[i]]; vector<int> ans; for(int i = 0; i < sLen-wordsSize*perWordLen+1; i++) { map<string, int> comparedMap; int j = 0; while(j < wordsSize) { string word = s.substr(i+j*perWordLen, perWordLen); if(baseMap.find(word) != baseMap.end()) { ++comparedMap[word]; if(comparedMap[word] > baseMap[word]) break; } else break; ++j; } if(j == wordsSize) ans.push_back(i); } return ans; }};
阅读全文
0 0
- [Leetcode]_30 Substring with Concatenation of All Words
- LeetCode: Substring with Concatenation of All Words
- LeetCode : Substring with Concatenation of All Words
- [Leetcode] Substring with Concatenation of All Words
- [LeetCode]Substring with Concatenation of All Words
- LeetCode-Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- LeetCode:Substring with Concatenation of All Words
- Leetcode: Substring with Concatenation of All Words
- [LeetCode] Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- leetcode Substring with Concatenation of All Words
- LeetCode Substring with Concatenation of All Words
- LeetCode | Substring with Concatenation of All Words
- LeetCode - Substring with Concatenation of All Words
- Substring with Concatenation of All Words -- LeetCode
- Leetcode: Substring with Concatenation of All Words
- leetcode:Substring with Concatenation of All Words
- Bootstrap3.0学习第十八轮(JavaScript插件——下拉菜单)
- 简易电话簿实现二(动态)
- psring_Struts2_Spring3整合问题
- nginx 配置中文详解
- Android常用布局管理器总结
- [Leetcode]_30 Substring with Concatenation of All Words
- 棋盘问题
- Bootstrap3.0学习第十九轮(JavaScript插件——标签页)
- cmd中运行打包文件,可以查看报错信息
- C++ std::mem_fn
- 首秀Hadoop伪分布式集群搭建流程
- hdu5996 dingyeye loves stone
- Java的重写与调用父类方法问题
- 计算机专业毕业生留给新生的一封公开信