当前位置:网站首页>Force deduction 76 questions, minimum covering string
Force deduction 76 questions, minimum covering string
2022-06-25 07:50:00 【Yingtai night snow】
Power button 76 topic , Minimum overlay string
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 "" .
Be careful :
- about
tDuplicate characters in , The number of characters in the substring we are looking for must not be less thantThe number of characters in the . - If
sThere is such a substring in , We guarantee that it is the only answer .
I/o sample
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 .
solution : The sliding window
string minWindow(string s,string t)
{
int slength=s.size();
int tlength=t.size();
// establish hash To store s,t Value in character
unordered_map<char,int>shash,thash;
// take t The characters of the string are saved to thash in
for(char temp:t)
{
thash[temp]++;
}
// Defining variables , Left pointer and right pointer
int left=0,right=0;
// Define the difference between two strings
int distance=0;
// Minimum string length
int minLen=slength+1;
// Subscript of starting position
int begin=0;
// Create a sliding window
while (right<slength)
{
char temp=s[right];
// If in string t Does not exist in the s Corresponding elements in , Then continue to move the right pointer
if(thash.count(temp)==0)
{
right++;
continue;
}
// Add the current character to hash In the table , And move the right pointer
shash[temp]++;
//distance Indicates that the sliding window contains t Number of the same number of characters in , The number of single characters in the window is greater than t The number of characters corresponding to the number of characters in the
// Judge the difference between characters
// If s In the string is equal to the string t String in , Continue to increase
if(shash[temp]==thash[temp])
{
distance++;
}
right++;
// Handle the left pointer , Because strings t It can be repeated
while(distance==thash.size())
{
// Update the length of the minimum string
if(right-left<minLen)
{
minLen=right-left;
begin=left;
}
// If the string t non-existent s Corresponding elements in , Continue to move the left pointer
char ltemp=s[left];
if(thash.count(ltemp)==0)
{
left++;
continue;
}
//s The characters of ltemp The number is exactly the same as t The number of is equal , Means that the two just meet , The representative is going to be on the team
if(shash[ltemp]==thash[ltemp])
{
distance--;
}
shash[ltemp]--;
left++;
}
}
// Whether the matching is successful depends on whether the value of the minimum length changes
return (minLen==slength+1)?"":s.substr(begin,minLen);
}
边栏推荐
- SCM Project Training
- Kinsing双平台挖矿家族病毒分析
- Modular programming of LCD1602 LCD controlled by single chip microcomputer
- Technology blog | how to communicate using SSE
- VSCode很好,但我以后不会再用了
- 海思3559 sample解析:vio
- Can I open a stock account with a compass? Is it safe?
- Invalid Navicat scheduled task
- [single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
- 力扣76题,最小覆盖字串
猜你喜欢

无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略

一次弄清楚 Handler 可能导致的内存泄漏和解决办法

PCB board design - automatic layout 2021-10-15

Take you through the normalization flow of GaN

Leetcode daily question - 515 Find the maximum value in each tree row

The method of judging whether triode can amplify AC signal

Modular programming of LCD1602 LCD controlled by single chip microcomputer

Four software 2021-10-14 suitable for beginners to draw PCB

基于地面点稀少的LiDAR点云的茂密森林蓄积量估算

三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试
随机推荐
剑指 Offer II 027. 回文链表
@The difference between resource and @autowired annotation, why recommend @resource?
One "stone" and two "birds", PCA can effectively improve the dilemma of missing some ground points under the airborne lidar forest
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
50. Pow(x, n)-快速幂
Cglib dynamic proxy
点云智绘在智慧工地中的应用
Atlassian Confluence 远程代码执行漏洞(CVE-2022-26134漏洞分析与防护
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
力扣78:子集
1742. 盒子中小球的最大数量
Collection of common terms and meanings in forestry investigation based on lidar
Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
微信小程序开通客服消息功能开发
Share the process requirements for single-layer flexible circuit board
Mysql面试-执行sql响应比较慢,排查思路。
[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
How to use printf of 51 single chip microcomputer
(tool class) use SecureCRT as the communication medium
el-input实现尾部加字