要求:words的单词串起来的串在s的下标
思路:滑动窗口,跟438有点像,但是注意要从下一个字符开始
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
map<string,int> m;
map<string,int> tm;
for(auto s:words){
m[s]+=1;
tm[s]=0;
}
int len=words[0].length();
int totallen=words.size()*len;
int left=0,n=s.length(),right=0;
vector<int> ans;
while(right<n){
if(m[s.substr(right,len)]==0){
left+=1;//不在词典只能跳一位
right=left;
tm.clear();
}
else if(tm[s.substr(right,len)]==m[s.substr(right,len)]){
left+=1;//出现过了
right=left;
tm.clear();
}
else {
//没出现过
tm[s.substr(right,len)]+=1;
right+=len;
}
if(right-left==totallen){
tm[s.substr(left,len)]-=1;
ans.push_back(left);
left+=1;
right=left;
tm.clear();
}
}
return ans;
}
};