题目
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) in s 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).
解答
思路:定义两个map容器,结构都是
unordered_map<string,int> counts;
key是字符串,value是该字符串出现的个数。
先对words进行遍历,初始化counts。
那下面就要遍历s了,但是我们没必要遍历到最后,因为要匹配整个words,所以下标只需到s_len-words_num*words1_len
这个很重要,许多超时的都是遍历到最后了。
之后用另一个unordered_map (seen)来计数
unordered_map<string,int> seen;
假设某次遍历,开始下标是 i
对于words出现的每一个word,进行遍历,用j表示words中第j个word,并截取s中以i下标开始的字符串word,查看counts中是否包含这个字符串,如果包含,seen[word]就自增,否则(不包含),就是以i开始的字符串不满足,i++。
在上述过程中,seen[word]如果超过counts[word],即出现的次数多了,肯定也不满足,直接跳转到i+1。
当所有的seen[word]都不超过counts[word],这个表示seen中每个字符串的个数等于counts中字符串个数。把此时的下标i保存起来。
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
unordered_map<string,int> counts;
for(int i=0;i<words.size();i++)
{
counts[words[i]]++;//初始化,每个string的个数
}
int s_len=s.length(),words_num=words.size(),words1_len=words[0].size();
vector<int> indexs;
for(int i=0;i<s_len-words_num*words1_len+1;i++)//遍历字符串中的字符,只需要遍历到s_len-words_len*words1_len+1即可
{
unordered_map<string,int> seen;
int j=0;
for(;j<words_num;j++)
{
string word=s.substr(i+j*words1_len,words1_len);//遍历s中每个words1_len长的字符串
if(counts.find(word)!=counts.end())
{
seen[word]++;//用于计数
if(seen[word]>counts[word])//对应的word超过个数了
break;
}
else//不包含word
break;
}
if(j==words_num) //比较的个数达到words_num个,满足条件
indexs.push_back(i);
}
return indexs;
}
};