题意为给定字符串s,以及字符串数组words, 求s中包含words的连接子串(顺序无关)的所有索引。
正解:
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
if(words==null||words.length==0){
return null;
}
int lenth = words[0].length();
int size = words.length;
List<Integer> result = new ArrayList<Integer>();
HashMap<String, Integer> tempSet = new HashMap<String, Integer>();
for(int i=0;i<words.length;i++){
if (tempSet.containsKey(words[i])) {
tempSet.put(words[i], tempSet.get(words[i]) + 1);
} else {
tempSet.put(words[i], 1);
}
}
for (int i = 0; i <= s.length() - size * lenth; i++) {
if (check(s.substring(i, i + lenth * size), words, lenth,tempSet)) {
result.add(i);
}
}
return result;
}
public static boolean check(String s, String[] words, int length,HashMap<String,Integer> tempMap) {
HashMap<String, Integer> tempSet = (HashMap<String, Integer>) tempMap.clone();
for(int i=0;i<words.length;i++){
String tmp=s.substring(i*length,i*length+length);
if (!tempSet.containsKey(tmp)) {
return false;
}
if (tempSet.get(tmp) == 1) {
tempSet.remove(tmp);
} else {
tempSet.put(tmp, tempSet.get(tmp) - 1);
}
}
return true;
}
}
初始解法为:
public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
if(words==null||words.length==0){
return null;
}
int lenth = words[0].length();
int size = words.length;
List<Integer> result = new ArrayList<Integer>();
HashMap<String, Integer> tempSet = new HashMap<String, Integer>();
for(int i=0;i<words.length;i++){
if (tempSet.containsKey(words[i])) {
tempSet.put(words[i], tempSet.get(words[i]) + 1);
} else {
tempSet.put(words[i], 1);
}
}
for (int i = 0; i <= s.length() - size * lenth; i++) {
if (check(s.substring(i, i + lenth * size), words, lenth,tempSet)) {
result.add(i);
}
}
return result;
}
public static boolean check(String s, String[] words, int length,HashMap<String,Integer> tempMap) {
HashMap<String, Integer> tempSet = (HashMap<String, Integer>) tempMap.clone();
while (!"".equals(s) ) {
String tmp = s.substring(0, length);
if (!tempSet.containsKey(tmp)) {
return false;
}
if (tempSet.get(tmp) == 1) {
tempSet.remove(tmp);
} else {
tempSet.put(tmp, tempSet.get(tmp) - 1);
}
s = s.substring(length);
}
return true;
}
}
将初始解法中的while循环改成for循环,重点是将string的剪切操作转为根据索引定位子串的方法,减少了大量的操作,节约时间。
解题体会:
初始存map存的是HashSet没考虑words可能重复的可能
初始子串s的遍历游标i是跳跃式增长,没考虑可能子串之间有重叠的可能
初始以s的子串为对象存的map,最后为节约时间以常量words来存map,且每次比较时使用了map的一个拷贝,速度较快
总结:
后续需要考虑问题时尽量全面