public static String minWindow2(String s, String t){
if (t.length()>s.length()){
return "";
}
HashMap<Character, Integer> hashMap = new HashMap<>();
for (Character i:t.toCharArray()){
if (hashMap.containsKey(i)){
hashMap.put(i,hashMap.get(i)-1);
}else{
hashMap.put(i,-1);
}
}
//滑动窗口
int start=0;
int end=0;
int max=Integer.MAX_VALUE;
String res = "";
while (end<s.length()){
//不为null
if (hashMap.get(s.charAt(end))!=null){
hashMap.put(s.charAt(end),hashMap.get(s.charAt(end))+1);
//判断符合不
if (test18(hashMap)){
//将start往后移动
while (test18(hashMap)&&start<=end){
//更改max
if (end-start+1<max){
max = end-start+1;
res = s.substring(start,end+1);
}
if (hashMap.get(s.charAt(start))!=null){
//减去1
hashMap.put(s.charAt(start),hashMap.get(s.charAt(start))-1);
}
start++;
}
end++;
}else{
end++;
}
}else{
end++;
}
}
return res;
}
public static boolean test18(HashMap<Character, Integer> hashMap){
for (Integer i:hashMap.values()){
if (i<0){
return false;
}
}
return true;
}
最小覆盖子串
新生农民2025-04-13 13:58
相关推荐
橘颂TA17 小时前
【剑斩OFFER】算法的暴力美学——连续数组憧憬blog18 小时前
【Kiro开发集训营】拒绝“屎山”堆积:在 Kiro 中重构“需求-代码”的血缘关系e***749518 小时前
Spring Security 官网文档学习n***i9519 小时前
Java NIO文件操作星释19 小时前
Rust 练习册 72:多米诺骨牌与回溯算法笃行客从不躺平20 小时前
接口幂等性(Idempotency)Hero | 柒20 小时前
JAVA反射机制j***630820 小时前
Springboot项目中线程池使用整理likuolei20 小时前
Eclipse 创建 Java 接口q***547521 小时前
Spring Boot 经典九设计模式全览