当前位置:网站首页>1420 · 最小覆盖子串II
1420 · 最小覆盖子串II
2022-04-22 06:16:00 【yinhua405】
描述
给你一个字符串 S、一个字符串 T,S是循环的,请在字符串 S 里面找出:包含 T 所有字母的最小子串。
- 如果 S 中不存这样的子串,则返回空字符串 ""。
- 如果 S 中存在这样的子串,我们保证它是唯一的答案。
样例
示例1:
输入:
S = "abcdc"
T = “da”
输出: "dca"
解释:你可以将S旋转成 "bcdca",因此你的最小覆盖子串就是“dca”
示例2:
输入:
S = "ADOBECODEBANC"
T = "DAOC"
输出: "CADO"
解释:你可以将S旋转成"BECODEBANCADO", 因此最小覆盖子串就是“CADO”
string minWindowII(string &S, string &T) {
// Write your code here
S = S + S;
int tSize = T.size();
int sSize = S.size();
vector<bool>visited(sSize+1, false);
map<char, int> tMap;
for (int i = 0; i < tSize; i++)
{
auto it = tMap.find(T[i]);
if (it != tMap.end())
{
tMap[T[i]]++;
}
else
{
tMap[T[i]]=1;
}
}
int start = 0;
int end = 0;
int minStart=0;
int minRet=INT_MAX;
int tmpTsize = tSize;
map<char, int> tMapTmp=tMap;
while (start < sSize)
{
while (tmpTsize > 0 && end < sSize)
{
auto it = tMapTmp.find(S[end]);
if (it != tMapTmp.end())
{
it->second--;
if (it->second >= 0)
{
tmpTsize--;
}
}
end++;
}
while (tmpTsize == 0 && start < sSize)
{
if (end - start < minRet)
{
minRet = end - start;
minStart = start;
}
auto it = tMapTmp.find(S[start]);
if (it != tMapTmp.end())
{
it->second++;
if (it->second > 0)
{
tmpTsize++;
}
}
start++;
}
if (end == sSize)
{
if (start == 0)
{
break;
}
start++;
end = start;
if (visited[end] == true)
{
break;
}
visited[end] = true;
tMapTmp = tMap;
tmpTsize = tSize;
}
}
string sRet = "";
for (int i = minStart; minRet != INT_MAX && i < minRet + minStart; i++)
{
sRet += S[i];
}
return sRet;
}
void test()
{
//string S = "abcdc";
//string T = "da"; //"dca"
//string S = "ADOBECODEBANC";
//string T = "DAOC";
输出: "CADO"
//string S = "JCYHY";
//string T = "D";
//string ret = minWindowII(S, T);
// string S = "eaGxRUaIOJ";
// string T = "Oax";
//string S = "PYXLzrJYdmUHpbxmSjObMdTrIXeuKxtyneqFJkyYaCMBUiKdqZRMOHAUDGuqI";
//string T = "zHqXF";
FJkyYaCMBUiKdqZRMOHAUDGuqIPYXLz
zrJYdmUHpbxmSjObMdTrIXeuKxtyneqF
//string ret = minWindowII(S, T); //xRUaIlcGHiO
string S = "DwCBNuWUcAtrcqorVwOsILwgUNLTThdBtyZmeoGehPXGUarYCVqLjxysuzUM";
string T = "YqTdOZjYMM";
//YCVqLjxysuzUMDwCBNuWUcAtrcqorVwOsILwgUNLTThdBtyZmeoGehPXGUarYCVqLjxysuzUM
string ret = minWindowII(S, T);
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yinhua405/article/details/122466153
边栏推荐
- (二)Sql Server的基本配置以及使用Navicat连接Sql Server
- Choose any novel website, crawl any novel and save it in the form of Notepad.
- C语言 | 指针
- C语言 | 位运算符
- . net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]
- [DRC 23-20] Rule violation (REQP-1712) Input clock driver - Unsupported PLLE2_ADV connectivity.
- 为什么非主键索引的叶子结点存放的数据是主键值
- ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss
- Relationship between A5 transceiver signal VOD and pre emphasis adjustment
- What is the internal structure of stack frame?
猜你喜欢

Vivado generates and invokes EDF netlist files
![. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]](/img/c8/e7579543d7911a907ae19f357478d8.png)
. net learning notes - about Net core (1) [. NETCORE's project structure, five ways to transfer values to pages, and the use of log4net and NLog]

Complete a student information management system and systematically practice object-oriented, function, string and other knowledge. Realize the comprehensive application of knowledge. Use classes, fun

Idea does not display the run dashboard view window

Define a student class 1 to get the student's name: get_ Name() return type: STR 2 get student's age: get_ Age() return type: int 3 returns the highest score among the three subjects. get_ course()

(五) 使用Navicat创建数据库数据表并设置id自增

链表难题记录一

instanceof的使用说明及实例讲解

LeetCode - 4 - (接雨水、无重复字符的最长子串、分发糖果、二叉树的<前中后层>序遍历)

Vscode, this is enough
随机推荐
桥接模式下主机ping不通虚拟机
Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
Redis进阶
Define a student class 1 to get the student's name: get_ Name() return type: STR 2 get student's age: get_ Age() return type: int 3 returns the highest score among the three subjects. get_ course()
instanceof的使用说明及实例讲解
(二)Sql Server的基本配置以及使用Navicat连接Sql Server
LeetCode - 6 - (字符串相乘、下一个更大元素<ⅠⅡⅢ>、k个一组翻转链表)
Detailed tree array template -- Theory and code implementation
SQL复习语法笔记整理,新鲜出炉
在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样
机械设计知识点规划
Win10 modify command line default font
. net learning notes (I) -- introduction, advantages, design ideas, principles and applications of generics
ROS installation and foundation and introduction
76 · 最长上升子序列
C语言 | 指针
Quartus II prevents signals from being integrated
[number theory] prime number (V): Mason prime number (lucas_lehmer decision method)
Redis advanced
modelsim仿真加速注意点