Leetcode: Minimum Window Substring
2014-02-18 22:09
447 查看
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.
双指针的思想,先找到一个包含T的窗口,然后在满足条件(包含T)的情况下向后移动头指针,如果不能再移动则继续移动尾指针。这里用一个计数器统计字符串S中字符串T内字符出现的次数 - 可以是重复字符,只要总次数小于T中的重复次数 - 如果等于T的长度表明已经包含T。
====================第二次===================
第二次也写了一个半小时啊。。。
class Solution {
public:
string minWindow(string S, string T) {
int expected_chars[256] = {0};
int total_count = T.size();
for (int i = 0; i < total_count; ++i) {
++expected_chars[T[i]];
}
int found_count = 0;
int found_chars[256] = {0};
int min_length = S.size() + 1;
int min_start = -1;
for (int i = 0, start = 0; i < S.size(); ++i) {
if (expected_chars[S[i]] == 0) {
continue;
}
++found_chars[S[i]];
if (found_chars[S[i]] <= expected_chars[S[i]]) {
++found_count;
while (found_count == total_count) {
while (expected_chars[S[start]] == 0) {
++start;
}
int length = i - start + 1;
if (length < min_length) {
min_length = length;
min_start = start;
}
--found_chars[S[start]];
if (found_chars[S[start]] < expected_chars[S[start]]) {
--found_count;
}
++start;
}
}
}
return min_start == -1 ? "" : S.substr(min_start, min_length);
}
};
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.
双指针的思想,先找到一个包含T的窗口,然后在满足条件(包含T)的情况下向后移动头指针,如果不能再移动则继续移动尾指针。这里用一个计数器统计字符串S中字符串T内字符出现的次数 - 可以是重复字符,只要总次数小于T中的重复次数 - 如果等于T的长度表明已经包含T。
class Solution { public: string minWindow(string S, string T) { int chars_to_find[256] = {0}; for (int i = 0; i < T.size(); ++i) { ++chars_to_find[T[i]]; } int ret, minlen = S.size() + 1; int count = 0; int has_found[256] = {0}; for (int i = 0, start = 0; i < S.size(); ++i) { if (chars_to_find[S[i]] == 0) { continue; } ++has_found[S[i]]; if (has_found[S[i]] <= chars_to_find[S[i]]) { ++count; } if (count == T.size()) { // have found all the characters in T while (chars_to_find[S[start]] == 0 || has_found[S[start]] > chars_to_find[S[start]]) { if (has_found[S[start]] > chars_to_find[S[start]]) { --has_found[S[start]]; } ++start; } if (i - start + 1 < minlen) { ret = start; minlen = i - start + 1; } } } return minlen == S.size() + 1 ? "" : S.substr(ret, minlen); } };
====================第二次===================
第二次也写了一个半小时啊。。。
class Solution {
public:
string minWindow(string S, string T) {
int expected_chars[256] = {0};
int total_count = T.size();
for (int i = 0; i < total_count; ++i) {
++expected_chars[T[i]];
}
int found_count = 0;
int found_chars[256] = {0};
int min_length = S.size() + 1;
int min_start = -1;
for (int i = 0, start = 0; i < S.size(); ++i) {
if (expected_chars[S[i]] == 0) {
continue;
}
++found_chars[S[i]];
if (found_chars[S[i]] <= expected_chars[S[i]]) {
++found_count;
while (found_count == total_count) {
while (expected_chars[S[start]] == 0) {
++start;
}
int length = i - start + 1;
if (length < min_length) {
min_length = length;
min_start = start;
}
--found_chars[S[start]];
if (found_chars[S[start]] < expected_chars[S[start]]) {
--found_count;
}
++start;
}
}
}
return min_start == -1 ? "" : S.substr(min_start, min_length);
}
};
相关文章推荐
- leetcode第一刷_Minimum Window Substring
- leetcode[76] Minimum Window Substring
- LeetCode题解:Minimum Window Substring
- Leetcode_minimum-window-substring(c++ version)
- leetcode Minimum Window Substring
- Minimum Window Substring leetcode java
- LeetCode刷题(49)--Minimum Window Substring
- leetcode — minimum-window-substring
- LeetCode-Minimum Window Substring
- 【leetcode】Minimum Window Substring
- LeetCode Minimum Window Substring(two pointer)
- 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/ 209 Minimum Size Subarray Sum
- LeetCode-Minimum Window Substring-最小窗口子串-滑动窗口算法(尺取法)
- LeetCode : Minimum Window Substring
- [leetcode-76]Minimum Window Substring(java)