感觉有点像暴力循环破解法,虽然能accepted,但有点慢
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
Set<String> number = new HashSet<>();
List<Integer> list = new ArrayList<>();
int len = words[0].length();//有几个字符串
int allLen = words.length*len;//words中所有字符串加起来的长度
String keyWord="";
HashMap<String,Integer> map = new HashMap<>();
for (String word:words) {
map.put(word,map.getOrDefault(word,0)+1);
}
for (String word:words) {
number.add(word);
}
//枚举法开始,从开头到结尾枚举从start字段到(start➕allLen)下标字段(这部分刚刚好是allLen长度的字符串)是不是符合情况,这个逻辑蛮简单。
for (int i =0 ; i<s.length()-allLen+1;i++){
HashMap<String,Integer> hashMap = new HashMap(map);
int cc = number.size();
for (int start=i; start<i+allLen;start+=len){
keyWord = s.substring(start,start+len);
if(hashMap.containsKey(keyWord)){
hashMap.put(keyWord,hashMap.get(keyWord)-1);
if (hashMap.get(keyWord)==0){
cc--;
}else if (hashMap.get(keyWord)<0){
break;
}else {
continue;
}
}
else break;
}
if (cc==0)list.add(i);
}
return list;
}
}