Leetcode: Minimum Window Substring
来源:互联网 发布:手机炒股软件排行 编辑:程序博客网 时间:2024/05/17 05:16
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.
两个指针+字符hash
string minWindow(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function int nT = T.size(); int nS = S.size(); int needToFind[256] = {0}; for (int i = 0; i < nT; ++i) ++needToFind[T[i]]; int hasFound[256] = {0}; int minBegin; int minEnd; int minWindow = nS + 1; int count = 0; char ch; for (int begin = 0, end = 0; end < nS; ++end) { if (needToFind[S[end]] == 0) continue; ch = S[end]; ++hasFound[ch]; if (hasFound[ch] <= needToFind[ch]) ++count; if (count == nT) { while (needToFind[S[begin]] == 0 || hasFound[S[begin]] > needToFind[S[begin]]) { if (hasFound[S[begin]] > needToFind[S[begin]]) --hasFound[S[begin]]; ++begin; } int length = end - begin + 1; if (length < minWindow) { minBegin = begin; minEnd = end; minWindow = length; } } } return minWindow <= nS ? S.substr(minBegin, minEnd-minBegin+1) : ""; }
- 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
- [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
- CSS中border的用法
- C/C++中指针和引用之相关问题研究
- javascript替换字符串中的某个汉字
- java 适配器模式
- 学习忘记
- Leetcode: Minimum Window Substring
- 找出数组中第二大的数
- HTML中调用FUNCTION coma函数
- 漫谈设计模式:从面向对象开始(奋斗的小鸟)_PDF 电子书
- [各种面试题] 黑白迷阵
- 常用算法五(分支限界法)
- IPhone UIButton 属性
- 设计模式之简单工厂模式
- IPsec