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).
这道题看似比较复杂,其实思路和 Longest Substring Without Repeating Characters 差不多。因为那些单词是定长的,所以本质上和单一个字符一样。和 Longest Substring Without Repeating Characters 的区别只在于我们需要维护一个字典,然后保证目前的串包含字典里面的单词有且仅有一次。思路仍然是维护一个窗口,如果当前单词在字典 中,则继续移动窗口右端,否则窗口左端可以跳到字符串下一个单词了。假设源字符串的长度为n,字典中单词的长度为l。因为不是一个字符,所以我们需要对源 字符串所有长度为l的子串进行判断。做法是i从0到l-1个字符开始,得到开始index分别为i, i+l, i+2*l, ...的长度为l的单词。这样就可以保证判断到所有的满足条件的串。因为每次扫描的时间复杂度是O(2*n/l)(每个单词不会被访问多于两次,一次是窗 口右端,一次是窗口左端),总共扫描l次(i=0, ..., l-1),所以总复杂度是O(2*n/l*l)=O(n),是一个线性算法。空间复杂度是字典的大小,即O(m*l),其中m是字典的单词数量。代码如 下:
public ArrayList findSubstring(String S, String[] L) {
ArrayList res = new ArrayList();
if(S==null || S.length()==0 || L==null || L.length==0)
return res;
HashMap map = new HashMap();
for(int i=0;i curMap = new HashMap();
int count = 0;
int left = i;
for(int j=i;j<=S.length()-L[0].length();j+=L[0].length())
{
String str = S.substring(j,j+L[0].length());
if(map.containsKey(str))
{
if(curMap.containsKey(str))
curMap.put(str,curMap.get(str)+1);
else
curMap.put(str,1);
if(curMap.get(str)<=map.get(str))
count++;
else
{
while(curMap.get(str)>map.get(str))
{
String temp = S.substring(left,left+L[0].length());
curMap.put(temp,curMap.get(temp)-1);
if(curMap.get(temp)
这种移动窗口的方法在字符串处理的问题中非常常见,是一种可以把时间复杂度降低到线性的有效算法,大家可以熟悉一下。还有非常类似的题目
Minimum Window Substring
,思路完全一样,只是移动窗口的规则稍微不同而已。
vector<int> findSubstring(string S, vector<string> &L) {
// Note: The Solution object is instantiated only once.
map<string,int> words;
map<string,int> cur;
int wordNum = L.size();
for(int i = 0; i < wordNum; i++)
words[L[i]]++;
int wordLen = L[0].size();
vector<int> res;
//if(S.size() < wordLen*wordNum)return res;
for(int i = 0; i <= (int)S.size()-wordLen*wordNum; i++)
{
cur.clear();
int j;
for(j = 0; j < wordNum; j++)
{
string word = S.substr(i+j*wordLen, wordLen);
if(words.find(word) == words.end())
break;
cur[word]++;
if(cur[word]>words[word])
break;
}
if(j == wordNum)
res.push_back(i);
}
return res;
}
class Solution {
private:
vector<int> res;
map<string,int> cntL;
map<string,int> cn;
int n ;
public:
vector<int> findSubstring(string S, vector<string> &L)
{ res.clear();
cntL.clear();
cn.clear();
n = S.length();
int e = L.size();
int t = L[0].length();
int k = 0;
for(int i = 0; i < e ; i++)
{ if(cn.count(L[i]) == 0)
{ cn[L[i]] = 1;
k++;
}
else
{ cn[L[i]] += 1;
k++;
}
}
string tr ,du;
int r = 0;
int st = 0;
for(int j = 0 ; j < t ; j++)
{ r = 0; st = j;
for(int i = j; i < n; i += t)
{ tr = S.substr(i,t);
if( cn.count(tr) == 0 || cn[tr] == 0 )
{ cntL.clear();
r = 0;
st = i+t;
}
else if(cntL[tr] < cn[tr])
{ cntL[tr] += 1;
r++;
}
else
{ du = S.substr(st,t);
while(du != tr)
{ cntL[du]--;
r--;
st += t;
du = S.substr(st,t);
}
st += t;
}
if(r == k)
{ res.push_back(st);
du = S.substr(st,t);
cntL[du]--;
r--;
st += t;
}
}
cntL.clear();
}
sort(res.begin(),res.end());
return res ;
}
};
浙公网安备 33010602011771号