leetcode.30. Substring with Concatenation of All Words
来源:互联网 发布:怎么筛选excel数据 编辑:程序博客网 时间:2024/06/05 11:04
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) ins that is a concatenation of each word in words exactly once and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { if (words.empty()) return vector<int>(); vector<int> ret; //记录所给words中每个单词的出现次数 map<string, int> word_count; //每个单词的长度相同 int word_size = strlen(words[0].c_str()); int word_nums = words.size(); //所给匹配字符串的长度 int s_len = strlen(s.c_str()); for (int i = 0; i < word_nums; i++) ++word_count[words[i]]; int i, j; map<string, int> temp_count; for (i = 0; i < s_len - word_nums*word_size + 1; ++i) { temp_count.clear(); for (j = 0; j < word_nums; j++) { //检验当前单词是否属于words以及出现的次数是否一致 string word = s.substr(i + j*word_size, word_size); if (word_count.find(word) != word_count.end()) { ++temp_count[word]; //如果出现的次数与words不一致,则返回错误 if (temp_count[word] > word_count[word]) break; }//if else{ break; }//else }//for //所有words内的单词,在i起始位置都出现,则将下标i存入结果的vector中 if (j == word_nums) { ret.push_back(i); }//if }//for return ret; }};
0 0
- 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
- LeetCode|Substring with Concatenation of All Words
- 设计模式学习-单例模式
- React Native项目修改包名(Android版)
- 从客户端 中检测到有潜在危险的Request.Form值
- C语言中的static 详细分析
- hdu 2807 The Shortest Path(矩阵)
- leetcode.30. Substring with Concatenation of All Words
- Android RecyclerView实现瀑布流
- 在路由器上面按IP限速
- cocos2d-x项目实战
- mybatis学习笔记(零)mybatis学习之HelloWorld
- 英文不好,学Unity3D,一件小事搞疯我--(二)
- OpenGLES---初始化OpenGL-ES环境
- hibernate(3)
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别