[leetcode]76. 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).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
- If there is no such window in S that covers all characters in T, return the empty string
""
. - If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
题意:
给定字符串S 和 T, 求S中可以cover T所有元素的子集的最小长度。
Solution1: Two Pointers(sliding window)
1. scan String T, using a map to record each char's frequency
2. use [leftMost to i] to maintain a sliding window, making sure that each char's frequency in such sliding window == that in T
3. if mapS [S.charAt(start)] > mapT [S.charAt(start)] , it signs we can move sliding window
4. how to find the next sliding window? move leftMost, meanwhile, decrement mapS [S.charAt(start)] until we find each frequency in [start to i] == that in T
code
class Solution {
public String minWindow(String S, String T) {
String result = "";
if (S == null || S.length() == 0) return result;
int[] mapT = new int[256];
int[] mapS = new int[256];
int count = 0;
int leftMost = 0;
for(int i = 0; i < T.length(); i++){
mapT[T.charAt(i)] ++;
} for(int i = 0; i < S.length(); i++){
char s = S.charAt(i);
mapS[s]++;
if(mapT[s] >= mapS[s]){
count ++;
} if(count == T.length()){
while(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]){
if(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]){
mapS[S.charAt(leftMost)]--;
}
leftMost ++;
}
if(result.equals("") || i - leftMost + 1 < result.length()){
result = S.substring(leftMost, i+1);
}
}
}
return result;
}
}
二刷:
对于出现在S但不出现在T的那些“配角” character的处理,
最好的方式是,边扫S边用map将其频率一并记上。
这样,在判断 while(mapS[S.charAt(leftMost)] > mapT[S.charAt(leftMost)]) 这个逻辑的时候,
这些“配角”character会因为只出现在S但不出现在T
而直接被left++给做掉
class Solution {
public String minWindow(String s, String t) {
String result = "";
if(s == null || s.length() == 0 || s.length() < t.length()) return result; int [] mapT = new int [128];
for(Character c : t.toCharArray()){
mapT[c]++;
} int left = 0;
int count = t.length();
int[] mapS = new int[128];
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
mapS[c] ++ ;
if(mapT[c] >= mapS[c]){
count --;
}
if(count == 0){
while(mapS[s.charAt(left)] > mapT[s.charAt(left)]){
mapS[s.charAt(left)] --;
left++;
}
if (result.equals("") || i - start + 1 < result.length()) {
result = s.substring(start, i + 1);
}
}
}
return result;
}
}
[leetcode]76. Minimum Window Substring最小字符串窗口的更多相关文章
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [Leetcode] minimum window substring 最小字符窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Leetcode#76 Minimum Window Substring
原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- 刷题76. Minimum Window Substring
一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...
- 【LeetCode】76. Minimum Window Substring
Minimum Window Substring Given a string S and a string T, find the minimum window in S which will co ...
- [LeetCode] Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【一天一道LeetCode】#76. Minimum Window Substring
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- telnet-redis-quit
很多时候 telnet 完就无法退出了,ctrl+c 有时也无法退出后来找到了正确的命令 ctrl+] 然后在telnet 命令行输入 quit 就可以退出了 telnet Trying 10.168 ...
- 我发起了一个 操作系统 GUI 和 Tcp / IP 包 的 开源项目 DeviceOS
操作系统 如果 不需要 处理 复杂多样 的 硬件 兼容性, 其实 并不算 大项目, 可以算 毕业设计 . 但是, GUI 和 Tcp / IP 这两个 部分 的 实现逻辑 很多 很复杂, 这 2 ...
- Linux touch命令详解
Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...
- [转]python中pandas库中DataFrame对行和列的操作使用方法
转自:http://blog.csdn.net/u011089523/article/details/60341016 用pandas中的DataFrame时选取行或列: import numpy a ...
- tinycc update VERSION to 0.9.27
TinyCC全称为Tiny C Compiler, 是微型c编译器,可在linux/win/平台上编译使用. 在用代码里面使用tcc当脚本,性能比lua还快,目前已有网游服务端使用TCC脚本提高性能. ...
- centos7如何安装部署Zabbix
参考http://www.cnblogs.com/momoshouhu/p/8041293.html 1.关闭selinux和firewall 1.1检测selinux是否关闭 [root@local ...
- Android之listview添加数据篇
一.ListView: 1. ListView通常有两个职责: 1.向布局填充数据 2.处理选择点击等操作 2.ListView的创建需要3个元素: 1. ListView中的每一列的View. 2. ...
- Python之Eric安装注意事项
处理该问题: http://www.knowsky.com/950133.html 注意缺乏的是qscintilla 双击install.py进行安装
- AI与RPA
RPA(机器人流程自动化)是一类自动化软件工具,它可以通过用户界面使用和理解企业已有的应用,将基于规则的常规操作自动化,例如读取邮件和系统,计算,生成文件和报告,检查文件等.因此,RPA的应用范围非常 ...
- 20165312 2017-2018-2《Java程序设计》第9周学习总结
20165312 2017-2018-2<Java程序设计>第9周学习总结 上周错题总结 1.进程的基本状态有:新建.运行.阻塞.死亡. A . true B . false 解析:A 这 ...