问题描述:
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).
分析:这道题很直观,可能要注意到的一点是words中是由重复的。我的想法比较直观。将字符串s从0到最后,然后查看以此为起点,将字符串切割为若干小的子字符串,然后查看hash表,是否存在,如果存在,对应值减1,当值为负的时候remove,当结束之后,如果hash表中不为空,那么就不是满足条件的字符串,如果为空,将索引加入res中。
代码如下:732ms
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new LinkedList<Integer>();
HashMap<String,Integer> strMapNums = new HashMap<String,Integer>();//key:string=>value:nums which string apperances
int sLen = s.length();
int subLen = words[0].length();
int subCount = words.length;
if(sLen<=0)
return res;
else if(subLen<=0)
return res;
//init strMapNums
for (String word : words) {
if (strMapNums.containsKey(word))
strMapNums.replace(word, strMapNums.get(word) + 1);
else
strMapNums.put(word, 1);
}
HashMap<String,Integer> tmpMap;
for(int i = 0;i<=sLen-subCount*subLen;i++){
tmpMap = (HashMap<String,Integer>)strMapNums.clone();
for(int j = 0;j<subCount;j++){
int index = i+subLen*j;
int end = index+subLen;
if(end>sLen)
break;
String substr = s.substring(index,end);
if(!tmpMap.containsKey(substr))
break;
int num = tmpMap.get(substr);
if(--num<=0)
tmpMap.remove(substr);
else
tmpMap.replace(substr,num);
}
if(tmpMap.isEmpty()){//查看是否有相同元素
res.add(i);
}
}
return res;
}
}
前面的做法时间实在是太长了,通过甚至都有侥幸的成分在。看了网友的博客(不好意思,找不到原文了),前面的部分和本人是一模一样的。到了循环体部分,他的做法比本人要高明。它使用的是滑动窗口的算法,因为substr长度是一样的。既然是一样的,那就可以进行切割。那总共有subLen种切割方法(subLen是word的长度),对于每种切割方法,维护一个引用计数和一个左起窗口(引用计数,限制窗口的宽度)。这样的好处是在中间的某些地方,可以不必重复检查。
代码如下:496ms
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> res = new LinkedList<Integer>();
HashMap<String,Integer> strMapNums = new HashMap<String,Integer>();//key:string=>value:nums which string apperances
int sLen = s.length();
int subLen = words[0].length();
int subCount = words.length;
if(sLen<=0)
return res;
else if(subLen<=0)
return res;
//init strMapNums
for (String word : words) {
if (strMapNums.containsKey(word))
strMapNums.replace(word, strMapNums.get(word) + 1);
else
strMapNums.put(word, 1);
}
for(int i = 0;i<subLen;i++){
int count = 0;
HashMap<String,Integer> curMap = (HashMap<String,Integer>)strMapNums.clone();
int left = i;
for(int j = i;j<=sLen-subCount*subLen;j+=subLen){
int startIndex = j;
int endIndex = j+subLen;
String substr = s.substring(startIndex,endIndex);
if(curMap.containsKey(substr)){
int num = curMap.get(substr)-1;
curMap.replace(substr, num);
if(num>=0) {
count++;
}
else {//出现重复,将左边界右移
while(true) {
String tmpSubStr = s.substring(left, left + subLen);
int tmpNum = curMap.get(tmpSubStr) + 1;
curMap.replace(tmpSubStr, tmpNum);
left = left + subLen;
count--;
if (tmpNum == 0)
break;
}
}
}else{
count = 0;
left = j+subLen;
curMap = (HashMap<String,Integer>)strMapNums.clone();
}
if(count==subCount){
res.add(left);
String tmpSubStr = s.substring(left,left+subLen);
int num = curMap.get(tmpSubStr)+1;
curMap.replace(tmpSubStr,num);
left = left+subLen;
count --;
}
}
}
return res;
}
}