Minimum Window Substring
来源:互联网 发布:网络信息安全检查表 编辑:程序博客网 时间:2024/05/29 14:04
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.
思路:因为O(n)的时间复杂度,所以考虑用双指针。
class Solution {public: string minWindow(string S, string T) { int tmplate[128] = {0}; int find_chars[128] = {0}; int t_count = T.size(); for(int i = 0; i < t_count; i++) { tmplate[T[i]]++; } int min_length = S.size() + 1; int min_start = -1; int count = 0; int start = 0; // start相当于前指针 for(int i = 0; i < S.size(); i++) { //i是后指针 if(tmplate[S[i]] == 0){ continue; } find_chars[S[i]]++; if(find_chars[S[i]] <= tmplate[S[i]]) { count++; } while(count == t_count) { //当找到一次完整的tmplate后处理 while(tmplate[S[start]] == 0) { start++; } int tmp_length = i - start + 1; //得到当前最小的subsr if(tmp_length < min_length) { min_length = tmp_length; min_start = start; } find_chars[S[start]]--; //移动前指针的三个步骤,首先对应的这个字符的统计个数减去1 if(find_chars[S[start]] < tmplate[S[start]]) { //去除前指针对应的字符后,对组合成template的影响,如果变少,count要减 count--; } start++; //移动前指针 } } return min_start == -1 ? "": S.substr(min_start, min_length); }};
Ref: http://blog.csdn.net/ymhhym/article/details/19432339
0 0
- LeetCode: Minimum Window Substring
- LeetCode Minimum Window Substring
- LeetCode : Minimum Window Substring
- [LeetCode] Minimum Window Substring
- [Leetcode] Minimum Window Substring
- Minimum Window Substring
- LeetCode: Minimum Window Substring
- Minimum Window Substring
- [LeetCode]Minimum Window Substring
- Minimum Window Substring
- Minimum Window Substring
- [leetcode]Minimum Window Substring
- Minimum Window Substring
- Leetcode:Minimum Window Substring
- LeetCode-Minimum Window Substring
- [leetcode] Minimum Window Substring
- [leetcode] Minimum Window Substring
- leetcode Minimum Window Substring
- wuQZ a5gtgray ban australia IqIp 5nI
- WrDT DElcBOakley Sunglasses australia vLDZ 9qQ
- 8jMK axCPgnike XyaV uu4
- IrBt V09aAray ban australia APrL Mth
- 23种常用设计模式(C++)
- Minimum Window Substring
- Valid Palindrome
- Palindrome Partitioning
- 完善Cygwin git使用
- 安装ubuntu笔记本博通无线网卡不可用
- 正则表达式与通配符基础
- Struts环境搭建
- 关于网络中tcp/ip的一些疑问
- Populating Next Right Pointers in Each Node