[leetcode] 76 Minimum Window Substring
2015-01-28 16:44
225 查看
问题描述:
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.
代码:
string minWindow(string S, string T) { //C++ int size_s = S.size(); int size_t = T.size(); if(size_t == 0||size_s==0||size_s<size_t) return ""; string result; map<char,int> dic; map<char,int> window; int validNum = 0,min = INT_MAX; for(int i=0; i<size_t; i++) dic[T[i]]++; for(int fast=0,slow=0; fast<size_s;fast++){ char ch = S[fast]; if(dic.count(ch)!=0){ window[ch]++; if(window[ch] <= dic[ch]) validNum++; } if(validNum == size_t){ while(dic.count(S[slow])==0||dic[S[slow]]<window[S[slow]]){ if(window.count(S[slow])!=0) window[S[slow]]--; slow++; } if(fast-slow+1 < min){ min = fast-slow+1; result = S.substr(slow,min); } } } return result; }
相关文章推荐
- leetcode 76:Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- Leetcode 76 Minimum Window Substring(最小窗口的子串)
- LeetCode-76-Minimum Window Substring 尺取法+字典
- LeetCode76 Minimum Window Substring(两种解法)
- Leetcode-76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring (尺取法 推荐)
- LeetCode76 Minimum Window Substring(两种解法)
- LeetCode_OJ【76】Minimum Window Substring
- LeetCode 76: Minimum Window Substring
- [leetcode-76]Minimum Window Substring(java)
- leetcode[76]Minimum Window Substring
- 【leetcode 76】 Minimum Window Substring
- 【LeetCode 76】 Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- leetcode 76 Minimum Window Substring
- LeetCode 76 Minimum Window Substring
- LeetCode 76 minimum window substring
- LeetCode76 Minimum Window Substring(两种解法)