[LeetCode] Minimum Window Substring
2013-01-29 20:35
393 查看
class Solution { public: string minWindow(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int m = S.size(); int n = T.size(); vector<int> TC(256); for (int i = 0; i < n; i++) TC[T[i]]++; int chs = 0; for (int i = 0; i < 256; i++) if (TC[i]) chs++; int pos = 0; int len = INT_MAX; int i = 0; for (int j = 0; j < m; j++) { char ch = S[j]; TC[ch]--; if (TC[ch] == 0) chs--; while (TC[S[i]] < 0) { TC[S[i]]++; i++; } if (chs == 0 && j - i + 1 < len) { len = j - i + 1; pos = i; } } return len == INT_MAX ? "" : S.substr(pos, len); } };
Small Case: 12ms
Large Case: 56ms
Time: O(n)
Space: O(n)
相关文章推荐
- leetcode:Minimum Window Substring
- [Leetcode] Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- [Leetcode] Minimum Window Substring My Submissions Question Solution
- LeetCode: Minimum Window Substring
- LeetCode刷题笔记(贪心):minimum-window-substring
- 143_leetcode_Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode: Minimum Window Substring [076]
- LeetCode OJ Minimum Window Substring
- LeetCode--Minimum Window Substring
- LeetCode Minimum Window Substring
- [LeetCode] Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- leetcode: Minimum Window Substring
- Leetcode-76 Minimum Window Substring
- leetcode hard模式专杀之76 Minimum Window Substring
- [Leetcode]Substring with Concatenation of All Words & Minimum Window Substring
- 【LeetCode】Minimum Window Substring
- Leetcode-Minimum Window Substring