LeetCode 30. Substring with Concatenation of All Words
来源:互联网 发布:linux就该这么学 下载 编辑:程序博客网 时间:2024/06/05 15:35
问题
https://leetcode.com/problems/substring-with-concatenation-of-all-words/
解法
经过测试,words中的字符串存在重复的情况。
因此第一步是统计每种字符串个数。
然后在s中进行枚举,比较当前串是否符合。
在枚举s时使用滑动窗口算法。
typedef unordered_map<string, int>::iterator it;class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { vector<int> ret; if (words.size() < 1) return ret; int wdLen = words[0].size(); if (s.size() <wdLen* words.size()) return ret; unordered_map<string, int> wdCnt; for(int i=0; i< words.size(); ++i) wdCnt[words[i]]++; for (int i=0; i<wdLen; ++i) { int left = i; unordered_map<string, int> found; int foundNum = 0; for(int j = i; j + wdLen<=s.size(); j+= wdLen) { string sub = s.substr(j, wdLen); if(wdCnt.find(sub) == wdCnt.end()) { found.clear(); foundNum =0; left = j+wdLen; }else{ found[sub]++; foundNum++; if (found[sub] > wdCnt[sub]) { while(found[sub] > wdCnt[sub]) { string ss = s.substr(left, wdLen); found[ss]--; foundNum--; left+= wdLen; } } if (foundNum == words.size()) { ret.push_back(left); string ss = s.substr(left, wdLen); found[ss]--; foundNum--; left+=wdLen; } } } } return ret; }};
实现细节
unordered_map<> clear()
Clear content
All the elements in the unordered_map container are dropped: their destructors are called, and they are removed from the container, leaving it with a size of 0.
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
- Qt编写信息管理系统(3)
- HttpClient简介
- Linux常用命令 经典版
- 正则表达式大全
- 正则表达式参考文档
- LeetCode 30. Substring with Concatenation of All Words
- Android 的快速开发框架XUtils
- hihocoder 练习 A+B
- Android动画常用公共属性详解
- 译--ToolBar(四)
- Windows DLL编程中的导入导出:__declspec(dllimport) ,__declspec(dllexport) ,
- NGUI PK UGUI
- Quartz Configuration Reference
- Ubuntu下shell脚本运行异常:bash和dash的区别