*leetcode #76 in cpp
2016-06-01 10:23
239 查看
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
Solution:
The method uses two pointers. One is pointing to the window's start and one is pointing to the window's end. Call these two pointers 'start' and 'end'. We move the start and end pointers to locate the minimum window.
There are 4 basic steps to locate the window.
1. find a valid window: we move end pointer to a position where s[start....end] has all characters in T within it.
2. shrink the valid window: we try to shrink the window, by moving the start pointer to the right, as small as possible which still contains all characters in T.
3. compare to current minimum window: compare the length of the shrinked window length and the current minimum window length. If the shrinked one has smaller length, update the minimum window to current one.
4. move to the next window: we move end to the right by 1. And redo step 1,2,3,4 again.
Code:
class Solution {
public:
string minWindow(string s, string t) {
int need[256] = {0};//record how many characters we need for string t. need[i] represents the number of i we need.
int found[256] = {0};//record how many characters we found in the window. found[i] represents the number of i we have found in the window
//construct need[]
for(int i =0; i < t.length(); i ++){
need[t[i]] ++;
}
int start = 0;//start of the window
int end =0; //end of the window
int has_found_num = 0;//number of characters in T we have found in the window. Say T is "ab", current window is "aab" then has_found_num = 2, that is 'a' and 'b'.
int res_start = 0;//final start ind of the minimum window
int res_end = INT_MAX;//final end ind of the minimum window
while(end<s.length()){// loop until end reaches the end of s
//if s[end] is not in t, it does not contribute to has_found_num and we don't need this character. Just skip it
if(!need[s[end]]) {
end++;
continue;
}
if(found[s[end]] < need[s[end]]){//if this is the character we need to construct a window for T, check if this is extra. Say t = "aab" and s[end] is the second 'a'. Then we need 'a' but this 'a' is extra. It does not contribute to has_found_num. If s[end] is extra, we record found[s[end]] +1 since we want to remember this letter has extra number. the extra number informaion would be needed in shrinking the window to minimum window
has_found_num ++;
}
found[s[end]] ++;
if(has_found_num == t.length()){//we have found a window which contains all characters
//currently start .....end contains all characters we need for t. But it still contains extra characters. We'll see if we can shrink this window by moving start to right.
//start to shrink the window
//start trying to shrink the window
while(found[s[start]] > need[s[start]] || found[s[start]]==0 ){
//1.if s[start]'s number is more than we need in the window, we can kick this out of this window and still have all characters for T. thus we kick it out.
//2. if s[start]'s number equal to how many we need, then we could not kick this out. Thus we could not shrink the window any more! we quit this loop
//. if s[start] is a letter we do not need for T, just kick it out.
if(found[s[start]] > need[s[start]]) found[s[start]]--;
start ++;
}
//after shirnking the window, see if this window is shorter than the current recorded minimum winow
if(end- start < res_end - res_start){
res_start = start;
res_end = end;
}
}
end++;//we move the end to right and check next window.
}
if(res_end == INT_MAX)
return "";
else return s.substr(res_start, res_end - res_start + 1);
}
};
For example,
S =
"ADOBECODEBANC"
T =
"ABC"
Minimum window is
"BANC".
Solution:
The method uses two pointers. One is pointing to the window's start and one is pointing to the window's end. Call these two pointers 'start' and 'end'. We move the start and end pointers to locate the minimum window.
There are 4 basic steps to locate the window.
1. find a valid window: we move end pointer to a position where s[start....end] has all characters in T within it.
2. shrink the valid window: we try to shrink the window, by moving the start pointer to the right, as small as possible which still contains all characters in T.
3. compare to current minimum window: compare the length of the shrinked window length and the current minimum window length. If the shrinked one has smaller length, update the minimum window to current one.
4. move to the next window: we move end to the right by 1. And redo step 1,2,3,4 again.
Code:
class Solution {
public:
string minWindow(string s, string t) {
int need[256] = {0};//record how many characters we need for string t. need[i] represents the number of i we need.
int found[256] = {0};//record how many characters we found in the window. found[i] represents the number of i we have found in the window
//construct need[]
for(int i =0; i < t.length(); i ++){
need[t[i]] ++;
}
int start = 0;//start of the window
int end =0; //end of the window
int has_found_num = 0;//number of characters in T we have found in the window. Say T is "ab", current window is "aab" then has_found_num = 2, that is 'a' and 'b'.
int res_start = 0;//final start ind of the minimum window
int res_end = INT_MAX;//final end ind of the minimum window
while(end<s.length()){// loop until end reaches the end of s
//if s[end] is not in t, it does not contribute to has_found_num and we don't need this character. Just skip it
if(!need[s[end]]) {
end++;
continue;
}
if(found[s[end]] < need[s[end]]){//if this is the character we need to construct a window for T, check if this is extra. Say t = "aab" and s[end] is the second 'a'. Then we need 'a' but this 'a' is extra. It does not contribute to has_found_num. If s[end] is extra, we record found[s[end]] +1 since we want to remember this letter has extra number. the extra number informaion would be needed in shrinking the window to minimum window
has_found_num ++;
}
found[s[end]] ++;
if(has_found_num == t.length()){//we have found a window which contains all characters
//currently start .....end contains all characters we need for t. But it still contains extra characters. We'll see if we can shrink this window by moving start to right.
//start to shrink the window
//start trying to shrink the window
while(found[s[start]] > need[s[start]] || found[s[start]]==0 ){
//1.if s[start]'s number is more than we need in the window, we can kick this out of this window and still have all characters for T. thus we kick it out.
//2. if s[start]'s number equal to how many we need, then we could not kick this out. Thus we could not shrink the window any more! we quit this loop
//. if s[start] is a letter we do not need for T, just kick it out.
if(found[s[start]] > need[s[start]]) found[s[start]]--;
start ++;
}
//after shirnking the window, see if this window is shorter than the current recorded minimum winow
if(end- start < res_end - res_start){
res_start = start;
res_end = end;
}
}
end++;//we move the end to right and check next window.
}
if(res_end == INT_MAX)
return "";
else return s.substr(res_start, res_end - res_start + 1);
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解