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. 集合总结二(LinkedList的实现原理)

    一.概述 先来看看源码中的这一段注释,我们先尝试从中提取一些信息: Doubly-linked list implementation of the List and Deque interfaces ...

  2. OpenLDAP一登录系统就修改密码

    1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepath /usr/lib/openldap modulepath /usr/lib64/openldap   ...

  3. sqlserver乱码问题解决

    * 如果是自己创建的数据库那么就应该在一开始就选择排序规则中的:Chinese_PRC_CI_AS 1.改变排序规则方法: 右击创建的数据库,属性→选项→排序规则中选择:Chinese_PRC_CI_ ...

  4. Excel技巧--单列变多行

    当上图的单列转变成多行时,可以这么做: 1.在第一行输入A2.A3,向右拖拉第一行: 2.第二行按第一行最右顺序,写下A12,再向右拖拉出第二行: 3.选择这两行,再拖拉出一串连续顺序的多行来: 4. ...

  5. ubuntu命令行打开网页

    在Ubuntu下,当需要打开其他格式文件时,比如pdf.jpg.mp3等格式文件,通常做法是进入到文件所在的目录,双击打开,很影响效率.事实上,可以通过命令xdg-open打开这些格式文件,甚至是网页 ...

  6. vagrant up报错 Warning: Authentication failure. Retrying...解决方案

    参照链接 https://www.cnblogs.com/zqifa/p/vagrant-1.html 可以解决问题.

  7. springJdbc(jdbcTemplate)事物拦截失效问题解决

    先贴上web.xml和spring-jdbc.xml代码: web.xml代码: <context-param> <param-name>contextConfigLocati ...

  8. ijkplayer总结

    12.ijkplayer的使用过程: 11.ijkpalyer引言: ==== 12.ijkplayer的使用过程: >>举例mac系统编译.so文件:   ijkplayer默认是不支持 ...

  9. 从Tomcat的处理web请求分析Java的内存模型

    Tomcat作为一个java应用,同样是有主线程和子线程的.主线使用while(true)的方式一直循环,等待客户端来连接.一个客户端来了之后,就从线程池中拿一个线程来处理请求,如果没有配置线程池,就 ...

  10. day28元类与异常查找

    元类与异常处理1. 什么是异常处理    异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常    没有被应用程序处理,那么该异常就会抛出来,程序的执行也随之终止    异常包含三个部分: ...