当前位置:网站首页>【Leetcode】76. 最小覆盖子串
【Leetcode】76. 最小覆盖子串
2022-06-26 09:36:00 【later_rql】
1.题目描述
给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 “” 。
测试用例:
输入:s = “ADOBECODEBANC”, t = “ABC”
输出:“BANC”
输入:s = “a”, t = “a”
输出:“a”
输入: s = “a”, t = “aa”
输出: “”
解释: t 中两个字符 ‘a’ 均应包含在 s 的子串中,
因此没有符合条件的子字符串,返回空字符串。
注意:
1. 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
2. 如果 s 中存在这样的子串,我们保证它是唯一的答案。
2.解题思路
(1)首先用两个哈希表ht和hs分别记录t和s中窗口的元素。cnt记录s数组中有多少元素符合t,当cnt==t.length,说明窗口s数组包含t中的元素。
(2)len用来记录包含t的最短子串长度,ans记录每次符合条件的子串。
执行过程如下图:
第一包含t数组:
第二次包含t数组:
第三次包含t数组:
3.代码
//滑动窗口+哈希表
public String minWindow2(String s, String t) {
HashMap<Character,Integer> hs = new HashMap<Character,Integer>();
HashMap<Character,Integer> ht = new HashMap<Character,Integer>();
for(int i = 0;i < t.length();i ++){
ht.put(t.charAt(i),ht.getOrDefault(t.charAt(i), 0) + 1);
}
String ans = "";
int len = 0x3f3f3f3f, cnt = 0; //有多少个元素符合
for(int i = 0,j = 0;i < s.length();i ++)
{
hs.put(s.charAt(i), hs.getOrDefault(s.charAt(i), 0) + 1);
if(ht.containsKey(s.charAt(i)) && hs.get(s.charAt(i)) <= ht.get(s.charAt(i))) cnt ++;
while(j < i && (!ht.containsKey(s.charAt(j)) || hs.get(s.charAt(j)) > ht.get(s.charAt(j))))
{
int count = hs.get(s.charAt(j)) - 1;
hs.put(s.charAt(j), count);
j ++;
}
if(cnt == t.length() && i - j + 1 < len){
len = i - j + 1;
ans = s.substring(j,i + 1);//截取j到i+1(不包括i+1)
}
}
return ans;
}
来源:力扣(LeetCode)
链接:link-76最小覆盖子串
边栏推荐
- The 100000 line transaction lock has opened your eyes.
- Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)
- A list of common methods for customizing paint and canvas of view
- The basis of C language grammar -- function definition learning
- Full introduction to flexboxlayout (Google official flexible implementation of flow layout control)
- From TF 1 X to TF 2.6 (update if you encounter it)
- logback
- 存储过程测试入门案例
- Single sign on logic
- Redis novice introduction
猜你喜欢

Deep learning (tentsorflow2. version) three good student performance problems (1)

install realsense2: The following packages have unmet dependencies: libgtk-3-dev

My creation anniversary

Upgrade idea to 2021.2 shortcut keys

WGCLOUD的web ssh服务端口是多少

MapReduce&Yarn理论

Differences between JVM, Dalvik and art

Notes on sports planning on November 22, 2021

国际化配置

國際化配置
随机推荐
Battery historian analyzes battery consumption
The first problem troubleshooting process of re disk
Crawler related articles collection: pyppeter, burpsuite
Automated testing -- Introduction and use of pytest itself and third-party modules
Jupyter Notebook遇到的问题
Extracting public fragments from thymeleaf
Differences between JVM, Dalvik and art
自动化测试——关于unitest与pytest初始化共存问题
定制拦截器
druid数据源实现后台监控
Common SQL add / delete / modify query statements
Specific implementation comparison between different programming languages
调用api接口生成不同颜色的微信小程序二维码
The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions
Openxcap usage
How to find and install the dependent libraries of Debian system
Threadmode interpretation of eventbus
TensorFlow动态分配显存
How to create an IE tab in edge browser
Throttling, anti chattering, new function, coriolism