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最小字符串窗口的更多相关文章

  1. [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 ...

  2. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  3. [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 ...

  4. Leetcode#76 Minimum Window Substring

    原题地址 用两个指针分别记录窗口的左右边界,移动指针时忽略那些出现在S种但是没有出现在T中的字符 1. 扩展窗口.向右移动右指针,当窗口内的字符即将多于T内的字符时,停止右移 2. 收缩窗口.向右调整 ...

  5. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  6. 刷题76. Minimum Window Substring

    一.题目说明 题目76. Minimum Window Substring,求字符串S中最小连续字符串,包括字符串T中的所有字符,复杂度要求是O(n).难度是Hard! 二.我的解答 先说我的思路: ...

  7. 【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 ...

  8. [LeetCode] Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. 【一天一道LeetCode】#76. Minimum Window Substring

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. Spring各个jar包作用

    Spring AOP:Spring的面向切面编程,提供AOP(面向切面编程)的实现Spring Aspects:Spring提供的对AspectJ框架的整合Spring Beans:Spring IO ...

  2. java 各种循环遍历

    遍历方式选择: 实现了 RandomAccess 接口的 list,优先选择普通 for 循环 ,其次 foreach: 未实现 RandomAccess 接口的 list, 优先选择 iterato ...

  3. Logistic Loss的简单讨论

    首先应该知道Logistic Loss和Crossing Entropy Loss本质上是一回事. 所以所谓的SoftMaxLoss就是一般二分类LogisitcLoss的推广.之所以在网络中采取这种 ...

  4. jquery,attr,prop,checkbox标签已有checked=checked但是不显示勾选

    最近在做项目的过程中碰到了这样的一个问题:在使用bootstrap模态框的过程中,在模态框中有一个checkbox标签,一开始是为选中的,当点击触发模态框按钮,选中chcekbox时,会显示勾选,这个 ...

  5. docker镜像制作 centos6 nginx1.15.6 with NGINX_UPSYNC_MODULE

    首先我选择了在centos6里部署nginx的镜像,如果大家选择的是centos7,自己重新修改吧 这里的问题点有几个: 1,make的版本选择,因为我下载了最新的cmake,需要c++11编译 这玩 ...

  6. PP.io的三个阶段,“强中心”——“弱中心”——“去中心”

    什么是PP.io? PP.io是我和Bill发起的存储项目,目的在于为开发者提供一个去中心化的存储和分发平台,能做到更便宜,更高速,更隐私. 当然做去中心化存储的项目也有好几个,FileCoin,Si ...

  7. Sql server 编写99乘法表

    Sql 组织编写语句 declare @one int,@tow int,@str varchar(100),@num intselect @one=1while(@one<=9)beginse ...

  8. Python—requests模块详解

    1.模块说明 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支持使用co ...

  9. ipv6无网络访问权限怎么办

    有时IP4和IP6都正常连接,但突然又出现“IPV6无网络访问权限” 这是win7系统下经常发生的事情,如下图. 方法/步骤 1.IPV6没网络权限是正常的因为你没有IPV6的网络环境,那个只有部分教 ...

  10. 实战ELK(6)使用logstash同步mysql数据到ElasticSearch

    一.准备 1.mysql 我这里准备了个数据库mysqlEs,表User 结构如下 添加几条记录 2.创建elasticsearch索引 curl -XPUT 'localhost:9200/user ...