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).
假设L中的单位长度为n,依次从S中取长度为n的子串,如果在L中,就记下来。如果在L中,但是已经匹配过了,说明匹配重复,也需要从S的下一个位置开始重新匹配,因为匹配要是连续的,中间不允许插入其他不匹配或者重复匹配的单词。需要借助hash或map,如果整个L都匹配完了,就算是一个concatenation;当匹配错误的时候,S右移一个位置。
C++实现代码:
#include<iostream> #include<vector> #include<string> #include<map> using namespace std; class Solution { public: vector<int> findSubstring(string S, vector<string> &L) { vector<int> ret; int n=L[0].size(); if(S.length()<n*L.size()) return ret; map<string,int> mp;//记录L中每一个string出现的次数 map<string,int> word; //记录在S中找到的L中string的次数,如果大于mp中的,说明找到重复的 size_t i; for(i=0;i<L.size();i++) mp[L[i]]++; size_t idx=0; //idx之后至少还剩下L中所有字符串的长度 while(idx<=S.length()-n*L.size()) { word.clear(); int flag=true; //i之后至少还剩下一个字符串的长度n,要取n长度的子串 for(i=idx;i<=idx+n*(L.size()-1);i+=n) { string tmp=S.substr(i,n); if(mp.find(tmp)==mp.end()) { flag=false; break; } word[tmp]++; if(word[tmp]>mp[tmp]) { flag=false; break; } } if(flag) ret.push_back(idx); idx++; } return ret; } }; int main() { Solution s; vector<string> L={"foo", "bar"}; vector<int> result=s.findSubstring(string("barfoothefoobarman"),L); for(auto a:result) cout<<a<<" "; cout<<endl; }