LEETCODE: Substring with Concatenation of All Words
来源:互联网 发布:平板怎么登录淘宝卖家 编辑:程序博客网 时间:2024/06/06 02:20
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9].
(order does not matter).
class Solution {public: vector<int> findSubstring(string S, vector<string> &L) { int lengthL = L[0].length() * L.size(); vector<int> results; int gap = S.length() - lengthL;// We do not want to waste time, // so if the remain is less than the total, no need to calculate. if(gap < 0) return results; map<string, int> word_count; // One word may appears more than one time. for (int ii = 0; ii < L.size(); ++ii) { ++word_count[L[ii]]; } map<string, int> counting; for(int ii = 0; ii <= gap; ii ++) { // Loop through S from 0 to gap. counting.clear(); int jj = ii; for(; jj < ii + lengthL; jj += L[0].length()) { // Process string from ii to ii + lengthL. string sub = S.substr(jj, L[0].length()); if(word_count.find(sub) != word_count.end()) { ++counting[sub]; if(counting[sub] > word_count[sub]) { break; } } else { break; } } if(jj == ii + lengthL) { results.push_back(ii); } } return results; }};
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
- Android编译系统详解(二)
- java实现页面加载更多功能
- VS2010 连接程序失败的解决办法
- 编辑手账 20141215 衆院選自公圧勝 重い信任を政策遂行に生かせ
- oracle中数据库,实例,表空间和用户之间的关系
- LEETCODE: Substring with Concatenation of All Words
- Android编译系统详解(三)
- Paraview Reader 的原理以及如何基于VTK写新的Reader
- mysql安装具体步骤
- 【Android Studio】Resource Merging资源合并规则
- centos 6.X下建立arduino开发环境
- Mac 下配置php环境
- android activity之间传递数据
- xp本地建立svn服务及应用