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
相关推荐
2601_956121971 小时前
背包基础篇(01、完全、分组、多重、混合)Java牛马1 小时前
SpringBoot Starter 依赖相关总结吴声子夜歌2 小时前
Java面试——Spring Cloud原理及应用(二)东小西2 小时前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来兴通物联科技3 小时前
SMT PCB 微小 DataMatrix 码扫不动问题分析 兴通 XT8601B 600 万像素工业读码器落地实践mqiqe3 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南脉动数据行情3 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)月华路5 小时前
G1 GC 对数组与大对象(Humongous)的处理我想走路带风5 小时前
LRU和最长前缀和(计算机网络算法)