zoukankan      html  css  js  c++  java
  • 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).

    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 empty string "".

    If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

    这种类型题目做法是创建一个target的数组,然后创建两个指针,一个start,一个end,在创建一个counter来标记是否遍历完T,此题有个难点就是考虑集中情况:

    S="AAB" T="AB";S="AB" T="AB";S="AOOB" T="AB"这三种情形。代码如下:

     1 public class Solution {
     2     public String minWindow(String s, String t) {
     3         int[] word = new int[128];
     4         for(char c:t.toCharArray()){
     5             word[c]++;
     6         }
     7         int begin = 0;
     8         int end = 0;
     9         int count = t.length();
    10         int len = Integer.MAX_VALUE;
    11         int head = 0;
    12         while(end<s.length()){
    13             if(word[s.charAt(end++)]-->0) count--;
    14             while(count==0){
    15                 if(end-begin<len) len = end-(head=begin);
    16                 if(word[s.charAt(begin++)]++==0) count++;
    17             }
    18         }
    19         System.out.println(head);
    20         return len==Integer.MAX_VALUE?"":s.substring(head,head+len);
    21     }
    22 }
  • 相关阅读:
    微软软件
    绘图软件安装出错解决方法
    Windows平台 Faster-RCNN 制作自己的数据集
    POJ2456 Agressive Cows
    P1030 求先序排列
    Luogu P2015二叉苹果树
    P2234 [HNOI2002]营业额统计
    Luogu P1347排序
    Luogu P1038神经网络
    Luogu P1006传纸条
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6493485.html
Copyright © 2011-2022 走看看