当前位置:网站首页>【Leetcode】76. Minimum covering substring
【Leetcode】76. Minimum covering substring
2022-06-26 10:09:00 【later_ rql】
76. Minimum cover substring
1. Title Description
Give you a string s 、 A string t . return s Covered by t The smallest string of all characters . If s There is no coverage in t Substring of all characters , Returns an empty string “” .
The test case :
Input :s = “ADOBECODEBANC”, t = “ABC”
Output :“BANC”
Input :s = “a”, t = “a”
Output :“a”
Input : s = “a”, t = “aa”
Output : “”
explain : t Two characters in ‘a’ Shall be included in s In the string of ,
Therefore, there is no qualified substring , Returns an empty string .
Be careful :
1. about t Duplicate characters in , The number of characters in the substring we are looking for must not be less than t The number of characters in the .
2. If s There is such a substring in , We guarantee that it is the only answer .
2. Their thinking
(1) First use two hash tables ht and hs Record separately t and s Window elements in .cnt Record s How many elements in the array match t, When cnt==t.length, Description window s The array contains t The elements in .
(2)len Used to record the contents of t The shortest substring length of ,ans Record the substring that meets the condition each time .
The execution process is as follows :
The first contains t Array :
The second contains t Array :
Third inclusion t Array :
3. Code
// The sliding window + Hashtable
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; // How many elements match
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);// Intercept j To i+1( barring i+1)
}
}
return ans;
}
source : Power button (LeetCode)
link :link-76 Minimum cover substring
边栏推荐
- 2. 合并两个有序数组
- TensorFlow遇到的各种错误
- Battery historian analyzes battery consumption
- echo $?
- Do you know the //go: instructions in the go source code, go:linkname?
- MySQL learning summary
- c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
- 【无标题】
- #云原生征文# 在 Google Kubernetes Cluster 上使用 HANA Expression Database Service
- Allocation de mémoire tas lors de la création d'objets
猜你喜欢

测试须知——常见接口协议解析

首批12家企业入驻!广州首个集中展销老字号产品专柜开张

MySQL learning summary

Test instructions - common interface protocol analysis
![[trajectory planning] testing of ruckig Library](/img/c7/51c0f6dc3bf7c7fa4528118a4c32fa.png)
[trajectory planning] testing of ruckig Library

逻辑英语结构【重点】

Day 3 array, pre post, character space, keyword and address pointer

国际化配置

Software testing - how to select the appropriate orthogonal table

WGCLOUD的web ssh服务端口是多少
随机推荐
#云原生征文# 在 Google Kubernetes Cluster 上使用 HANA Expression Database Service
Control setting layout in linear layout_ Gravity doesn't work?
Configuration internationale
install opencv-contrib-dev to use aruco code
[trajectory planning] testing of ruckig Library
The basis of C language grammar -- pointer (multidimensional array, function, summary) learning
Svn command
Deep learning (tentsorflow2. version) three good student performance problems (1)
cmake / set 命令
創建對象的時候堆內存的分配
Opencv depthframe - > pointcloud causes segmentation fault!
Problems encountered by jupyter notebook
Software testing - how to select the appropriate orthogonal table
Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)
online trajectory generation
字符串常量池、class常量池和运行时常量池
美国总统签署社区安全法案以应对枪支问题
P1296 whispers of cows (quick row + binary search)
字符串常量池、class常量池和运行时常量池
Daily-used English phrases