LeetCode Substring with Concatenation of All Words
2014-05-29 13:41
337 查看
题目
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:
L:
You should return the indices:
(order does not matter).
寻找所有正好包含所有单词(没有多余字符)的子串的头,不区分单词顺序。
注意:单词可以是重复的。
步骤O(n):
1、统计所有出现的单词及相应的数量,获取单词长度。
2、循环单词长度次,每次把头向后推移一个字符
2.1、刷新头位置,清空找到的单词,找到的单词总数
2.2、以单词长度为单位,依次向后获取单词。
(1)、如果不是要找的单词,清空统计数据,更新头位置,继续
(2)、如果是要找的单词,刷新相应单词计数
(2.1)如果该单词计数超过要找的数量,头向后移,减去头上的单词,直到减去的单词是尾部的单词
(2.2)如果总计数等于要找的单词数,暂存现在的头位置
3、返回所有暂存的头
代码:
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].
(order does not matter).
寻找所有正好包含所有单词(没有多余字符)的子串的头,不区分单词顺序。
注意:单词可以是重复的。
步骤O(n):
1、统计所有出现的单词及相应的数量,获取单词长度。
2、循环单词长度次,每次把头向后推移一个字符
2.1、刷新头位置,清空找到的单词,找到的单词总数
2.2、以单词长度为单位,依次向后获取单词。
(1)、如果不是要找的单词,清空统计数据,更新头位置,继续
(2)、如果是要找的单词,刷新相应单词计数
(2.1)如果该单词计数超过要找的数量,头向后移,减去头上的单词,直到减去的单词是尾部的单词
(2.2)如果总计数等于要找的单词数,暂存现在的头位置
3、返回所有暂存的头
代码:
class Solution { public: vector<int> findSubstring(string S, vector<string> &L) { vector<int> ans; int s_len=S.size(); //字符串长度 int l_len=L.size(); //单词数 if(s_len==0||l_len==0) return ans; int word_len=L[0].size(); //单词长度 if(s_len<l_len*word_len) return ans; int i,j; map<string,int> word_num; //单词的数量 for(i=0;i<l_len;i++) //获取单词及数量 word_num[L[i]]++; int first; //当前扫描串的头 map<int,int> found; //找到的单词 int count; //找到的总单词数(包括重复) int num; //当前扫描的单词应有的次数 string word,word1; //当前扫描的单词,头后移时扫描的单词 for(i=0;i<word_len;i++) //循环单词长度次 { found.clear(); count=0; first=i; for(j=i;j<s_len;j+=word_len) //扫描字符串 { word.clear(); word.append(S,j,word_len); num=word_num[word]; if(num==0) //不是需要的单词 { found.clear(); count=0; first=j+word_len; } else //是 { if(++found[word]>num) //超过相应单词数量 { while(first<j) { word1.clear(); word1.append(S,first,word_len); found[word1]--; count--; first+=word_len; if(word1==word) break; } } count++; if(count==l_len) //找到一个解 { ans.push_back(first); } } } } return ans; } };
相关文章推荐
- [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 Online Judge 题目C# 练习 - Substring with Concatenation of All Words
- leetcode 102: 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 71: 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