lintcode-medium-Minimum Window Substring

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

Notice

If there is no such window in source that covers all characters in target, return the emtpy string "".

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

Clarification

Should the characters in minimum window has the same order in target?

  • Not necessary.

Example

For source = "ADOBECODEBANC", target = "ABC", the minimum window is"BANC"

Challenge

Can you do it in time complexity O(n) ?

这题思路和minimum sum subarray差不多

public class Solution {
    /**
     * @param source: A string
     * @param target: A string
     * @return: A string denote the minimum window
     *          Return "" if there is no such a string
     */
    public String minWindow(String source, String target) {
        // write your code

        if(source == null || source.length() == 0 || target == null || target.length() == 0 || source.length() < target.length())
            return "";

        int[] tar = new int[128];
        int[] sou = new int[128];

        for(int i = 0; i < target.length(); i++)
            tar[target.charAt(i)]++;

        int left = 0;
        int right = 0;
        int start = -1;
        int end = source.length();
        int win = 0;

        int len = source.length();

        for(; right < len; right++){
            if(tar[source.charAt(right)] != 0){
                sou[source.charAt(right)]++;

                if(sou[source.charAt(right)] <= tar[source.charAt(right)]){
                    win++;
                }

                if(win == target.length()){
                    while(left < right){
                        if(tar[source.charAt(left)] == 0){
                            left++;
                        }
                        else if(sou[source.charAt(left)] > tar[source.charAt(left)]){
                            sou[source.charAt(left)]--;
                            left++;
                        }
                        else{
                            break;
                        }
                    }

                    win--;

                    if(right - left < end - start){
                        start = left;
                        end = right;
                    }
                }

            }
        }

        if(start == -1)
            return "";
        else
            return source.substring(start, end + 1);
    }
}
时间: 2024-11-07 01:32:16

lintcode-medium-Minimum Window Substring的相关文章

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

【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

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

[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

LeetCode &quot;Minimum Window Substring&quot; - STAR

Main reference: http://zhaohongze.com/wordpress/2014/01/04/leetcode-minimum-window-substring/ The ART of counting. So difficult and beautiful problem. It is all about dynamic window maintanence. Essentially, it is still a fancy linear scanning proced