当前位置:网站首页>力扣76题,最小覆盖字串
力扣76题,最小覆盖字串
2022-06-25 06:41:00 【瀛台夜雪】
力扣76题,最小覆盖字串
题目描述
给你一个字符串 s
、一个字符串 t
。返回 s
中涵盖 t
所有字符的最小子串。如果 s
中不存在涵盖 t
所有字符的子串,则返回空字符串 ""
。
注意:
- 对于
t
中重复字符,我们寻找的子字符串中该字符数量必须不少于t
中该字符数量。 - 如果
s
中存在这样的子串,我们保证它是唯一的答案。
输入输出样例
输入:s = "ADOBECODEBANC", t = "ABC"
输出:"BANC"
输入:s = "a", t = "a"
输出:"a"
输入: s = "a", t = "aa"
输出: ""
解释: t 中两个字符 'a' 均应包含在 s 的子串中,
因此没有符合条件的子字符串,返回空字符串。
解法:滑动窗口
string minWindow(string s,string t)
{
int slength=s.size();
int tlength=t.size();
//建立hash用以存储s,t字符中的值
unordered_map<char,int>shash,thash;
//将t字符串的字符保存到thash中
for(char temp:t)
{
thash[temp]++;
}
//定义变量,左指针和右指针
int left=0,right=0;
//定义两字符串的差异值
int distance=0;
//最小字串的长度
int minLen=slength+1;
//起始位置的下标
int begin=0;
//建立滑动窗口
while (right<slength)
{
char temp=s[right];
//如果在字符串t中不存在s中对应的元素,则继续移动右指针
if(thash.count(temp)==0)
{
right++;
continue;
}
//将当前字符添加到hash表中,并移动右指针
shash[temp]++;
//distance 表示滑动窗口内部包含t中相同字符数目的个数,窗口内单个字符个数大于t中对应的字符个数的时候不再增加
//判断字符之间的差异
//如果s字符串中的等于字符串t中的字符串,继续增加
if(shash[temp]==thash[temp])
{
distance++;
}
right++;
//对于左指针进行处理,因为字符串t可以是重复的
while(distance==thash.size())
{
//更新最小字符串的长度
if(right-left<minLen)
{
minLen=right-left;
begin=left;
}
//如果字符串t不存在s中对应的元素,则继续移动左指针
char ltemp=s[left];
if(thash.count(ltemp)==0)
{
left++;
continue;
}
//s的字符ltemp数目恰好与t的数目相等,代表两者刚好满足,代表要出队
if(shash[ltemp]==thash[ltemp])
{
distance--;
}
shash[ltemp]--;
left++;
}
}
//根据最小长度的值是否改变则决定是否匹配成功
return (minLen==slength+1)?"":s.substr(begin,minLen);
}
边栏推荐
- NSIS silent installation vs2013 runtime
- Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
- JDBC-DAO层实现
- NPM install reports an error: gyp err! configure error
- How to use printf of 51 single chip microcomputer
- Distributed quorum NWR of the alchemy furnace of the Supreme Master
- Explain distributed raft with dynamic diagram
- CPDA|数据分析师成长之路如何起步?
- Evolution of Alibaba e-commerce architecture
- My debut is finished!
猜你喜欢
Terms and concepts related to authority and authentication system
Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
不同路径II[针对DFS的动态规划改进]
Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
npm install 报错 : gyp ERR! configure error
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
随机推荐
神经网络与深度学习-3- 机器学习简单示例-PyTorch
Shell tips (134) simple keyboard input recorder
GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items
Chuantuwei ca-is3720lw alternative material No. iso7820fdw
ts环境搭建
OAuth 2.0一键登录那些事
Construction of occupancy grid map
Kinsing双平台挖矿家族病毒分析
STL教程4-输入输出流和对象序列化
Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes
JMeter introduction practice ----- use of global variables and local variables
判断用户是否是第一次进入某个页面
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection
Explain distributed raft with dynamic diagram
C#入门教程
图扑软件数字孪生 3D 风电场,智慧风电之海上风电
Function template_ Class template
Redis learning notes
Application of point cloud intelligent drawing in intelligent construction site
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]