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 wordsexactly once and without any intervening characters.
For example, given:
s:"barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
下面这段代码只能通过三分之二的测试用例,因为a concatenation of each word in wordsexactly once,我能说题目意思给的不明确么?
网上还有应用“滑动窗口”的时间复杂度为O(n)算法,后面再分析!
1 class Solution { 2 public: 3 vector findSubstring(string s, vector& words) { 4 vector res; 5 int wordNum=words.size(),wordLen=words[0].size(); 6 if(s.size() dic,curWord; 9 for(int i=0;i dic[word])25 break;26 }27 if(count==wordNum)28 res.push_back(i);29 }30 return res;31 }32 };