当前位置:网站首页>1420 · minimum coverage substring II
1420 · minimum coverage substring II
2022-04-22 07:29:00 【yinhua405】
describe
Give you a string S、 A string T,S It's cyclical , Please in string S Find it inside : contain T The youngest string of all letters .
- If S There is no such substring in , Returns an empty string "".
- If S There is such a substring in , We guarantee that it is the only answer .
Examples
Example 1:
Input :
S = "abcdc"
T = “da”
Output : "dca"
explain : You can take S Rotate into "bcdca", So your smallest covering substring is “dca”
Example 2:
Input :
S = "ADOBECODEBANC"
T = "DAOC"
Output : "CADO"
explain : You can take S Rotate into "BECODEBANCADO", Therefore, the minimum covering substring is “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";
Output : "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://yzsam.com/2022/04/202204220613324743.html
边栏推荐
- A. Alice and Bob (博弈?思维&暴力)(2021牛客暑期多校训练营1)
- Codeforces Round #610 (Div. 2)
- Codeforces Round #776 (Div. 3)
- LeetCode - 6 - (字符串相乘、下一個更大元素<ⅠⅡⅢ>、k個一組翻轉鏈錶)
- 内部类使用说明(静态、实例、局部)
- Cannot find interface mapping after updating HDF
- Leetcode - 2 - (parenthesis generation, longest palindrome string, ring linked list, reverse linked list, nodes in pairwise exchange linked list)
- Beyond compare solution to "authorization key has been revoked"
- A.Binary Seating (概率) (2021年度训练联盟热身训练赛第五场)
- Recursive reverse linked list
猜你喜欢

最小圆覆盖(计算几何基础)

队列(详解)——手撕队列习题

链表习题详解

Leetcode - 5 - (repeated substring < KMP >, longest palindrome substring, transpose matrix, binary tree (left and right) view)

L1-064 估值一亿的AI核心代码 (20 分) 格式错误

L2-005 集合相似度(set判重)

B. Cutting corners (simple geometry / sign in) (Game 5 of 2021 Training Alliance warm-up training competition)

instanceof的使用说明及实例讲解

LeetCode - 1 - (树的子结构、组合、螺旋矩阵、全排列<ⅠⅢ>)

L1-071 前世档案 (20 分) (类似二分)
随机推荐
1. Jam packed (Game 5 of 2021 training League warm-up training competition)
Singleton pool, singleton bean, singleton mode
Vivado generates and invokes EDF netlist files
Codeforces Round #780 (Div. 3)
1420 · 最小覆盖子串II
L1-071 前世档案 (20 分) (类似二分)
189. Rotation array
This关键字详细概述
2021 learning plan
(5) Use Navicat to create database data table and set ID auto increment
296 · array de duplication
CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes)
843 · 数字翻转
Redis進階
pip一直更新失败?多数是网络问题!
抽象类和抽象方法
A. Alice and Bob (game? Thinking & Violence) (2021 Niuke summer multi school training camp 1)
15 · 全排列
Links summary qwq
C language | preprocessing