最小子区间,不区分匹配字符串的顺序 如acbacb, ab ->输出 ba
private static boolean okWindows(int[] mapS, int[] mapT, List<Character> str) {
for(int i=0; i<str.size(); i++) {
if(mapS[str.get(i)] < mapT[str.get(i)])
return false;
}
return true;
}
private static String solve(String s, String t) {
int[] mapS = new int[128];
int[] mapT = new int[128];
List<Character> str = new ArrayList<>();
for(int i=0; i<t.length(); i++) {
mapT[t.charAt(i)]++;
}
for(int i=0; i<t.length(); i++) {
char ch = t.charAt(i);
if(mapT[ch] > 0)
str.add(ch);
}
String result = "";
int begin = 0;
for(int i=0; i<s.length(); i++) {
mapS[s.charAt(i)]++;
while(begin < i) {
//1.begin指向非匹配元素 如:cab, ab begin=0->c
char chBegin = s.charAt(begin);
if(mapT[chBegin] == 0) {
begin++;
//2.begin~i存在重复的匹配元素 如:acab begin=0->a(first)
} else if(mapS[chBegin] > 1) {
mapS[chBegin]--;
begin++;
} else {
break;
}
}
if(okWindows(mapS, mapT, str)) {
int newWinSize = i - begin + 1;
if("".equals(result) || newWinSize < result.length()) {
result = s.substring(begin, i+1);
}
}
}
return result;
}