leetcode- Minimum Window Substring
2014-09-25 16:43
417 查看
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S =
T =
Minimum window is
Note:
If there is no such window in S that covers all characters in T, return the emtpy string
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
class Solution {
public:
string minWindow(string S, string T) {
int n = S.size();
if(n == 0) return "";
int m = T.size();
if(m == 0) return "";
int num;
vector<int> a(52,0);
vector<bool> b(52,false);
int count = 0;
for(int i = 0; i < m; i++)
{
if(T[i] - 'a' >= 0)
{
num = T[i] - 'a';
}
else num = T[i] - 'A'+26;
if(a[num] == 0)
{
a[num] = 1;
count++;
b[num] = true;
}
else a[num]++;
}
int ret1 = 0, ret2 = n;
int l = 0, r = 0;
int c = 0;
bool exsit = false;
while(l < n)
{
if(c < count && r < n)
{
if(S[r] - 'a' >= 0)
{
num = S[r] - 'a';
}
else num = S[r] - 'A'+26;
r++;
if(!b[num])continue;
a[num]--;
if(a[num] == 0)
{
c++;
if(c == count)
{
if(r-l <= ret2-ret1)
{
ret1 = l;
ret2 = r;
exsit = true;
}
}
}
}
else
{
if(S[l] - 'a' >= 0)
{
num = S[l] - 'a';
}
else num = S[l] - 'A'+26;
l++;
if(b[num])
{
a[num]++;
if(a[num] > 0)
{
c--;
}
}
if(c == count && r-l < ret2-ret1)
{
ret1 = l;
ret2 = r;
}
}
}
if(!exsit) return "";
return S.substr(ret1, ret2-ret1);
}
};
For example,
S =
"ADOBECODEBANC"
T =
"ABC"
Minimum window is
"BANC".
Note:
If there is no such window in S that covers all characters in T, return the emtpy string
"".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
class Solution {
public:
string minWindow(string S, string T) {
int n = S.size();
if(n == 0) return "";
int m = T.size();
if(m == 0) return "";
int num;
vector<int> a(52,0);
vector<bool> b(52,false);
int count = 0;
for(int i = 0; i < m; i++)
{
if(T[i] - 'a' >= 0)
{
num = T[i] - 'a';
}
else num = T[i] - 'A'+26;
if(a[num] == 0)
{
a[num] = 1;
count++;
b[num] = true;
}
else a[num]++;
}
int ret1 = 0, ret2 = n;
int l = 0, r = 0;
int c = 0;
bool exsit = false;
while(l < n)
{
if(c < count && r < n)
{
if(S[r] - 'a' >= 0)
{
num = S[r] - 'a';
}
else num = S[r] - 'A'+26;
r++;
if(!b[num])continue;
a[num]--;
if(a[num] == 0)
{
c++;
if(c == count)
{
if(r-l <= ret2-ret1)
{
ret1 = l;
ret2 = r;
exsit = true;
}
}
}
}
else
{
if(S[l] - 'a' >= 0)
{
num = S[l] - 'a';
}
else num = S[l] - 'A'+26;
l++;
if(b[num])
{
a[num]++;
if(a[num] > 0)
{
c--;
}
}
if(c == count && r-l < ret2-ret1)
{
ret1 = l;
ret2 = r;
}
}
}
if(!exsit) return "";
return S.substr(ret1, ret2-ret1);
}
};
相关文章推荐
- LeetCode_Minimum Window Substring
- Leetcode Minimum Window Substring
- [LeetCode] Minimum Window Substring
- LeetCode Minimum Window Substring
- **LeetCode-Minimum Window Substring
- [leetcode 76] Minimum Window Substring
- LeetCode —— Minimum Window Substring
- [LeetCode] Minimum Window Substring 最小窗口子串
- LeetCode76 Minimum Window Substring(两种解法)
- Leetcode: Minimum Window Substring
- [leetcode] Minimum Window Substring
- LeetCode76 Minimum Window Substring
- Minimum Window Substring - LeetCode
- Leetcode: Minimum Window Substring
- LeetCode Minimum Window Substring
- Minimum Window Substring -- LeetCode
- leetcode -- Minimum Window Substring -- 重点,应该会考
- leetcode 日经贴,Cpp code -Minimum Window Substring
- [Leetcode] Minimum Window Substring
- [LeetCode] Minimum Window Substring