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
相关推荐
不会就选b32 分钟前
算法日常・每日刷题--<贪心>14野生技术架构师2 小时前
2026 Java 面试全套总结,八股 + 场景 + AI 相关面试考点香吧香2 小时前
java服务异常日志只打印异常类型,没有堆栈定位分析qq_2518364573 小时前
AI 疾病自查功能SpringbootAI项目实战 · 技术实现文档mmmmath_33 小时前
LeetCode.541.反转字符串IINavigator_Z3 小时前
LeetCode //MySQL - 1251. Average Selling Price逆境不可逃3 小时前
Pi Agent 学习笔记:对话太长以后,如何压缩上下文MetaLite4 小时前
JDK 8~26 核心特性一览:从 Stream 到 Scoped Valueswno7044 小时前
Spring Boot WebFlux增删改查