[leetcode]Minimum Window Substring
来源:互联网 发布:mysql 5.5.37 for mac 编辑:程序博客网 时间:2024/06/11 01:30
Minimum Window Substring
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.
解题思路:模拟TCP的滑动窗口协议,用两个哈希表记录窗口中的字符个数,其中一个作为标量记录目标串中字符的出现个数,
另一个作为变量记录当前源串中收集到的目标串上的字符个数,当且仅当第二个收集哈希表的有效字符个数与目标串的长度一致时,
就是一个有效窗口,计算其大小,重要的是要收缩其前端的开始下标,直到记录个数第一次小于目标串长度,然后继续扩展窗口大小
算法最大的时间复杂度O(n) 最多两次扫描
class Solution {public: string minWindow(string s, string t) { size_t n = s.size(); //源串长 size_t m = t.size(); //目标串长 //用来记录当前源串中所含目标串字符的个数,当且仅当于目标串相等时为一个有效窗口 int cnt = 0; int start = 0; //起始下标 int minStart = n; //最小窗口的起始下标 int minWin = n; //最小窗口大小 int stable[256]; //源串中的字符记数表 int ttable[256]; //目标串中字符的记数表 //初始化 for(size_t j = 0; j < 256; j++){ stable[j] = 0; ttable[j] = 0; } //统计t中各个字符的出现次数 for(size_t j = 0; j < m; j++){ ttable[t[j]]++; } //窗口滑动 size_t i; //初始化有效的起始下标 for(i = 0; i < n; i++){ if(ttable[s[i]] != 0){ start = i; break; } } for(; i < n; i++){ if(ttable[s[i]] != 0){ //统计源串字符在目标串中的个数,目标字符没有达到要求就继续统计 if(stable[s[i]] < ttable[s[i]]){ cnt++; } stable[s[i]]++; } if(cnt == m){ //有效窗口出现 //找出最小边界开始的地方,BBBBBA BA //收缩窗口 while(start < i){ if(ttable[s[start]] != 0 && cnt == m){ if(stable[s[start]] == ttable[s[start]]){ break; } //一定是stable[s[start]] < ttable[s[start]]的地方 stable[s[start]]--; } start++; } int tWin = i - start + 1; if(tWin <= minWin){ //计算最小窗口,并保留最小开始小标 minStart = start; minWin = tWin; } //收缩窗口 while(start < i){ if(ttable[s[start]] != 0 && cnt == m){ if(stable[s[start]] == ttable[s[start]]){ cnt--; } stable[s[start]]--; } start++; if(ttable[s[start]] != 0 || start >= i) break; } } } if(minStart == n) return ""; return string(s, minStart, minWin); }};
0 0
- 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
- KVO和Notification
- iOS开发-自定义后台显示图片(iOS7-Background Fetch的应用)
- python字典构造函数dict(mapping)解析
- java的一些基本概念
- 帆软与用友:共建安徽国资委财务信息系统
- [leetcode]Minimum Window Substring
- POJ 1047——解题报告
- vs如何向main函数传参数、设置字符编码、设置OpenMp等
- 使用lint提高android代码质量
- 算法——动态规划篇——最长公共子序列
- 九度 题目1366:栈的压入、弹出序列
- java多线程
- 用maven导出依赖jar
- ios上ZXing库的配置流程