leetcode - Minimum Window Substring
2014-10-26 16:56
316 查看
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.
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.
class Solution { public: std::string minWindow(std::string S, std::string T) { if (S.empty() || T.empty()) { return ""; } int count = T.size(); int require[128] = {0}; bool chSet[128] = {false}; for (int i = 0; i < count; ++i) { require[T[i]]++; chSet[T[i]] = true; } int i = -1; int j = 0; int minLen = INT_MAX; int minIdx = 0; while (i < (int)S.size() && j < (int)S.size()) { if (count) { i++; require[S[i]]--; if (chSet[S[i]] && require[S[i]] >= 0) { count--; } } else { if (minLen > i - j + 1) { minLen = i - j + 1; minIdx = j; } require[S[j]]++; if (chSet[S[j]] && require[S[j]] > 0) { count++; } j++; } } if (minLen == INT_MAX) { return ""; } return S.substr(minIdx, minLen); } };
相关文章推荐
- LeetCode (Minimum Window Substring)
- LeetCode()Minimum Window Substring 超时,但觉得很清晰。
- LeetCode76 Minimum Window Substring(两种解法)
- leetcode- Minimum Window Substring
- Minimum Window Substring(leetcode)
- 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
- [leetcode] Minimum Window Substring
- Leetcode: Minimum Window Substring
- LeetCode - Minimum Window Substring
- Minimum Window Substring 最小子串窗口@LeetCode
- LeetCode76 Minimum Window Substring(两种解法)