当前位置:网站首页>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
t
Duplicate characters in , The number of characters in the substring we are looking for must not be less thant
The number of characters in the . - If
s
There 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);
}
边栏推荐
- [daily training] 207 Class Schedule Card
- OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
- NSIS silent installation vs2013 runtime
- 神经网络与深度学习-3- 机器学习简单示例-PyTorch
- C#获取exe的版本号-文件版本and程序集版本
- Take you through the normalization flow of GaN
- The method of judging whether triode can amplify AC signal
- php入门基础记录
- 57. 插入区间
- What are the problems with traditional IO? Why is zero copy introduced?
猜你喜欢
剑指 Offer II 027. 回文链表
Function template_ Class template
VectorDraw Web Library 10.10
NPM install reports an error: gyp err! configure error
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
Four software 2021-10-14 suitable for beginners to draw PCB
VectorDraw Developer Framework 10.10
函数模板_类模板
El input to add words to the tail
realsense d455 semantic_slam实现语义八叉树建图
随机推荐
Share the process requirements for single-layer flexible circuit board
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
[little knowledge] PCB proofing process
Terms and concepts related to authority and authentication system
Access to foreign lead domain name mailbox
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
ts环境搭建
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
Five causes of PCB board deformation and six solutions 2021-10-08
基于RBAC 的SAAS系统权限设计
c# winform panel自定义图片和文字
Misunderstanding of switching triode
[Video] ffplay uses MJPEG format to play USB camera
The method of judging whether triode can amplify AC signal
"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
1464. 数组中两元素的最大乘积
个人域名和企业域名的区别
Modular programming of oled12864 display controlled by single chip microcomputer