LeetCode: Minimum Window Substring
2014-09-04 21:23
323 查看
[b]LeetCode: Minimum Window Substring[/b]
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.
地址:https://oj.leetcode.com/problems/minimum-window-substring/
算法:由于申请两个大小为256的数组,用于统计每一个字符串中各字母的个数,然后用isWindow函数来判断是否由hashS表示的字符串包含由hashT表示的字符串。首先,从S开始找到第一在T中出现的字母位置,记为i,初始化start为i,其中count表示在字符串S[start~i]之间出现在字符串T的字符个数。在每次进入循环时都判断S[start~i]是否可以包含T,如果包含,则根据情况更新最小窗口,并且更新start为下一个出现在字符串T中位置(因为最小的窗口的其实位置一定会出现在字符串T中),注意还要将改变hashS让其表示当前的S[start~i](注意i还是不变);如果不包含,则改变i为下一个出现在字符串T中的位置,并且同时更新对应的hashS。代码:
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 =
"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.
地址:https://oj.leetcode.com/problems/minimum-window-substring/
算法:由于申请两个大小为256的数组,用于统计每一个字符串中各字母的个数,然后用isWindow函数来判断是否由hashS表示的字符串包含由hashT表示的字符串。首先,从S开始找到第一在T中出现的字母位置,记为i,初始化start为i,其中count表示在字符串S[start~i]之间出现在字符串T的字符个数。在每次进入循环时都判断S[start~i]是否可以包含T,如果包含,则根据情况更新最小窗口,并且更新start为下一个出现在字符串T中位置(因为最小的窗口的其实位置一定会出现在字符串T中),注意还要将改变hashS让其表示当前的S[start~i](注意i还是不变);如果不包含,则改变i为下一个出现在字符串T中的位置,并且同时更新对应的hashS。代码:
class Solution { public: string minWindow(string S, string T) { int slen = S.size(); int tlen = T.size(); if(slen <= 0 || tlen <= 0) return string(); vector<int> hashT(256,0); vector<int> hashS(256,0); for(int i = 0; i < tlen; ++i) ++hashT[T[i]]; int minNum = slen + 1; int minStart = 0, minEnd = 0; int i = 0; while(i < slen && !hashT[S[i]]){ ++i; } if(i >= slen) return string(); int start = i; int count = 1; ++hashS[S[i]]; while(i < slen){ if(isWindow(hashT,hashS,count,tlen)){ if(minNum > i-start+1){ minNum = i-start+1; minStart = start; minEnd = i; } --count; --hashS[S[start]]; ++start; while(start<slen && !hashT[S[start]]) ++start; }else{ ++i; while(i<slen && !hashT[S[i]]) ++i; if(i < slen){ ++count; ++hashS[S[i]]; } } } if(minNum == slen + 1){ return string(); } return S.substr(minStart,minNum); } bool isWindow(vector<int> &hashT, vector<int> &hashS, int count ,int tlen){ if(count < tlen) return false; for(int i = 0; i < 256; ++i){ if(hashS[i] < hashT[i]){ return false; } } return true; } };
相关文章推荐
- leetcode hard模式专杀之76 Minimum Window Substring
- leetcode 76 Minimum Window Substring/ 209 Minimum Size Subarray Sum
- LeetCode 76: Minimum Window Substring
- LeetCode-Minimum Window Substring
- LeetCode OJ Minimum Window Substring
- [LeetCode] 058: Minimum Window Substring
- LeetCode Minimum Window Substring
- [LeetCode] Minimum Window Substring
- leetCode(55):Minimum Window Substring(limits.h头文件)
- LeetCode-Minimum Window Substring
- LeetCode: Minimum Window Substring
- leetcode第一刷_Minimum Window Substring
- LeetCode 76 Minimum Window Substring(Python详解及实现)
- Leetcode 76 Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode76——Minimum Window Substring
- Leetcode_minimum-window-substring(c++ version)
- LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- LeetCode--minimum-window-substring
- Minimum Window Substring leetcode java