当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

Manufacturing process of PCB 2021-10-11

VectorDraw Developer Framework 10.10

Application of point cloud intelligent drawing in intelligent construction site

What are the problems with traditional IO? Why is zero copy introduced?

海思3559 sample解析:vio

Construction of occupancy grid map

Modular programming of oled12864 display controlled by single chip microcomputer

VectorDraw Web Library 10.10

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

Technology blog | how to communicate using SSE
随机推荐
单位转换-毫米转像素-像素转毫米
OAuth 2.0 one click login
机器学习笔记 - 时间序列的线性回归
(tool class) quickly add time to code in source insight
【视频】ffplay 使用mjpeg格式播放usb摄像头
[Video] ffplay uses MJPEG format to play USB camera
navicat定时任务无效
【日常训练】207. 课程表
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
双三次差值bicubic
Function template_ Class template
年后求职找B端产品经理?差点把自己坑惨了......
Modular programming of LCD1602 LCD controlled by single chip microcomputer
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
VSCode很好,但我以后不会再用了
基于RBAC 的SAAS系统权限设计
Estimation of dense forest volume based on LIDAR point cloud with few ground points
Tips on how to design soft and hard composite boards ~ 22021/11/22
What are the problems with traditional IO? Why is zero copy introduced?