天题系列 Minimum Window Substring

压力太大了,这道题先不做了

public class Solution {
    public String minWindow(String S, String T) {
        // 讲解http://articles.leetcode.com/2010/11/finding-minimum-window-in-s-which.html
        //代码 http://www.cnblogs.com/huntfor/p/3915288.html
        String res = "";
        if(S==null || T==null) return "";

        int[]  found= new int[256];
        int[] needfind = new int[256];
        int minLen = S.length()+1;

        for(int i=0;i<T.length();i++){
            needfind[T.charAt(i)]++;
        }
        int left = 0, cnt =0;

        for(int i=0;i<S.length(); i++){ // i is right end
            if(needfind[S.charAt(i)]==0) continue;  // skip

            found[S.charAt(i)]++;
            if(found[S.charAt(i)] <= needfind[S.charAt(i)])
                cnt++;

            // while restraint reachs
            if(cnt==T.length()){
                while(left<S.length() &&(needfind[S.charAt(left)] ==0 || found[S.charAt(left)] >needfind[S.charAt(left)]) ){
                    if(found[S.charAt(left)] >needfind[S.charAt(left)])
                        found[S.charAt(left)]--;

                    left++;
                }

                int winlen = i-left+1;
                if(winlen<minLen){
                    minLen = winlen;
                    res = S.substring(left, i+1);
                }
            }
        }

        return res;
    }
}

总有一天我们回首曾经的痛苦和迷茫,会觉得那算个什么事儿

时间: 2024-08-11 07:49:00

天题系列 Minimum Window Substring的相关文章

【leetcode刷题笔记】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 i

[leetcode]Minimum Window Substring @ Python

原题地址:https://oj.leetcode.com/problems/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" M

Minimum Window Substring leetcode

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:

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

LeetCode: Minimum Window Substring [076]

[题目] 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

Minimum Window Substring &amp;&amp;&amp; Longest Substring Without Repeating Characters 快慢指针,都不会退,用hashmap或者其他结构保证

1 public class Solution { 2 public static int lengthOfLongestSubstring(String s) { 3 4 char[] arr = s.toCharArray(); 5 int pre = 0; 6 7 HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 8 9 for (int i = 0; i < arr.length;

076 Minimum Window Substring

076 Minimum Window Substring 这道题就是用一个dictionary来跟踪有多少还需要Match的字母以及当前需要match的次数. 再用一个queue 来记录哪些字幕被Match了以及他们被match的顺序 from collections import defaultdict class Solution: # @param {string} s # @param {string} t # @return {string} def minWindow(self, s

53. Minimum Window Substring

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:

Minimum Window Substring leetcode java

题目: 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 w