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

思路:

  Hashtable+双指针

我的代码:

public class Solution {
    public String minWindow(String s, String t) {
        if(s==null || s.length()==0)    return "";
        if(t==null || t.length()==0)    return "";
        int[] mark = new int[256];
        int[] valid = new int[256];
        for(int i=0; i<t.length(); i++)
        {
            mark[t.charAt(i)]++;
            valid[t.charAt(i)]++;
        }
        int minStart = 0;
        int minSize = Integer.MAX_VALUE;
        int start = 0;
        int count = 0;

        for(int end=0; end<s.length(); end++)
        {
            if(mark[s.charAt(end)] > 0)
            {
                valid[s.charAt(end)]--;
                if(valid[s.charAt(end)] >= 0) count++;
                if(count == t.length())
                {
                    while(true)
                    {
                        if(mark[s.charAt(start)] > 0)
                        {
                            if(valid[s.charAt(start)] < 0) valid[s.charAt(start)]++;
                            else break;
                        }
                        start++;
                    }
                    if(minSize > end-start+1)
                    {
                        minSize = end-start+1;
                        minStart = start;
                    }
                }
            }

        }
        if(minSize == Integer.MAX_VALUE) return "";
        return s.substring(minStart, minStart+minSize);
    }
}

学习之处:

  • 对于一个含有重复的对象集合,要像用Hash继续标记是否访问,用一个hash表显然是不够的,因为对象集合中含有重复元素啊,此时此刻常用的方式是用两个hash表,一个hash表用于标示每一个对象出现的次数,另外一个hash表用于标示每一个对象是否合乎前面一个hash表中的次数要求 一个填坑,一个挖坑
  • 所谓的双指针只指 一个首指针一个尾指镇,首指针用于确定上界,尾指针用于确定下界,如果题目要求是否是最小,确定好尾指针,首指针一步步的收缩,进而确定最小的范围。
  • 一天一天的改变自己不好的坏习惯
时间: 2024-11-05 11:27:29

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