[leetcode] Minimum Window Substring
2014-07-04 16:42
447 查看
tips:
count统计需要满足的各个string数目
found统计到目前为止已经包含的各个string数目
num统计满足count内的found数目(超出时不统计)
begin和end去维护满足条件的窗口首尾
保证满足条件的情况下更新begin和end,当begin位置的字符串found数目大于count时可后移,否则后移end直到begin可移动
代码如下:
class Solution {
public:
string minWindow(string S, string T) {
unordered_map<char, int> count;
unordered_map<char, int> found;
int index = 0, minLen = INT_MAX;
int num = 0;
for(int i = 0; i < T.length(); ++i)
count[T[i]] ++;
int begin = 0, end = 0;
while(begin < S.length() && count[S[begin]] == NULL)
begin++;
for(int i = begin; i < S.length(); ++i)
{
if(count[S[i]] != NULL)
{
found[S[i]]++;
if(found[S[i]] <= count[S[i]])
num++;
if(num == T.length())
{
end = i;
if(end - begin + 1 < minLen)
{
minLen = end - begin + 1;
index = begin;
}
break;
}
}
}
if(num != T.length())
return "";
while(end < S.length())
{
while(found[S[begin]] > count[S[begin]])
{
found[S[begin]]--;
begin++;
while(begin < S.length() && count[S[begin]] == NULL)
begin++;
if(end - begin + 1 < minLen)
{
minLen = end - begin + 1;
index = begin;
}
}
end++;
while(end < S.length() && count[S[end]] == NULL)
end++;
if(end < S.length())
found[S[end]]++;
}
if(minLen == INT_MAX)
return "";
return S.substr(index, minLen);
}
};
count统计需要满足的各个string数目
found统计到目前为止已经包含的各个string数目
num统计满足count内的found数目(超出时不统计)
begin和end去维护满足条件的窗口首尾
保证满足条件的情况下更新begin和end,当begin位置的字符串found数目大于count时可后移,否则后移end直到begin可移动
代码如下:
class Solution {
public:
string minWindow(string S, string T) {
unordered_map<char, int> count;
unordered_map<char, int> found;
int index = 0, minLen = INT_MAX;
int num = 0;
for(int i = 0; i < T.length(); ++i)
count[T[i]] ++;
int begin = 0, end = 0;
while(begin < S.length() && count[S[begin]] == NULL)
begin++;
for(int i = begin; i < S.length(); ++i)
{
if(count[S[i]] != NULL)
{
found[S[i]]++;
if(found[S[i]] <= count[S[i]])
num++;
if(num == T.length())
{
end = i;
if(end - begin + 1 < minLen)
{
minLen = end - begin + 1;
index = begin;
}
break;
}
}
}
if(num != T.length())
return "";
while(end < S.length())
{
while(found[S[begin]] > count[S[begin]])
{
found[S[begin]]--;
begin++;
while(begin < S.length() && count[S[begin]] == NULL)
begin++;
if(end - begin + 1 < minLen)
{
minLen = end - begin + 1;
index = begin;
}
}
end++;
while(end < S.length() && count[S[end]] == NULL)
end++;
if(end < S.length())
found[S[end]]++;
}
if(minLen == INT_MAX)
return "";
return S.substr(index, minLen);
}
};
相关文章推荐
- LeetCode76 Minimum Window Substring(两种解法)
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- [Leetcode]Substring with Concatenation of All Words & Minimum Window Substring
- [leetcode刷题系列]Minimum Window Substring
- Leetcode: Minimum window substring
- leetcode 076 —— Minimum Window Substring
- [leetcode] Minimum Window Substring
- [leetcode] 76 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
- [leetcode]Minimum Window Substring
- [leetcode] Minimum Window Substring
- LeetCode: Minimum Window Substring
- [leetcode]Minimum Window Substring
- [leetcode] Minimum Window Substring