Substring with Concatenation of All Words[leetcode]
来源:互联网 发布:京瓷网络扫描设置 编辑:程序博客网 时间:2024/05/01 14:37
思路是用一个map记录词典l中所有单词以及出现次数。
记录单词长度wl,词典中单词总长度ttl。
对于所有可行的开始位置,在O(s.size)时间内查找是否有满足要求的字符串。关键点为找到map[substr]==0,以及不存在的map[substr]
总的时间复杂度为O(s.size * wl)
vector<int> findSubstring(string s, vector<string> &l) { //init map<string, int> wordMap; vector<int> res; for (int i = 0; i < l.size();i++) { if (wordMap.find(l[i]) == wordMap.end()) wordMap[l[i]] = 0; wordMap[l[i]] ++; } int wl = l[0].size(); int ttl = wl * l.size(); //find for (int start = 0; start < wl; start++) { map<string, int>temp(wordMap); int l = start; for (int r = start; r + wl <= s.size(); r+= wl) { string curStr = s.substr(r, wl); if (temp.find(curStr) != temp.end()) { temp[curStr]--; if (temp[curStr] == 0 && r + wl - l == ttl) { res.push_back(l); temp[s.substr(l, wl)]++; l += wl; } while (temp[curStr] < 0) { temp[s.substr(l, wl)]++; l += wl; } } else { l = r + wl; temp = wordMap; if (l + ttl > s.size()) break; } } } return res; }
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
- 《Head First Design Patterns》读书笔记之单例模式
- 开辟一个二维数组,有10*8个元素,用随机数填充,按照下面的方法用函数实现查找一个数是否存在
- Map集合类中保证线程安全的策略比较
- SVM 为什么说高斯核函数能把向量映射到无穷维
- 主要内容
- Substring with Concatenation of All Words[leetcode]
- Qt5.3 OpenCV 环境搭建(编译OpenCV)
- javascript用户代理检测
- 一段有意思的java代码
- HDOJ 5012 Dice
- QT学习——QT命令行开发环境搭建的几个问题以及处理
- 有两个数a,b,请写一个函数交换a,b
- HDOJ 5014 Number Sequence
- 嵌入式开发的那些年--aiku