描述
给你一个字符串 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);
}