Java for LeetCode 076 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 emtpy string "".

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

解题思路:

涉及到查找操作,将T放进tMap中,同时创建一个保存T中所有字符(不重复)的图sMap,用一个计数器表示sMap和tMap的重合程度,一旦sMap包含了tMap即找到了一组解,通过收缩begin指针获取刚好包含tMap的位置,之后就是比较大小了,JAVA实现如下:

 public String minWindow(String s, String t) {
        HashMap<Character, Integer> tMap = new HashMap<Character, Integer>();
        HashMap<Character, Integer> sMap = new HashMap<Character, Integer>();
        for (int i=0;i<t.length();i++){
        	sMap.put(t.charAt(i), 0);
            if (!tMap.containsKey(t.charAt(i)))
                tMap.put(t.charAt(i), 1);
            else
                tMap.put(t.charAt(i), tMap.get(t.charAt(i)) + 1);
        }
        int begin=0,count=0,minBegin=0,length=s.length()+1;;
        for(int i=0;i<s.length();i++){
        	if(!tMap.containsKey(s.charAt(i)))
        		continue;
        	sMap.put(s.charAt(i), sMap.get(s.charAt(i))+1);
        	if(sMap.get(s.charAt(i))<=tMap.get(s.charAt(i)))
        		count++;
        	if(count==t.length()){
        		for(int j=begin;j<=i;j++){
        			if(!tMap.containsKey(s.charAt(j)))
        				continue;
        			if(sMap.get(s.charAt(j))>tMap.get(s.charAt(j))){
        				sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
        				continue;
        				}
        			sMap.put(s.charAt(j),sMap.get(s.charAt(j))-1);
        			count--;
        			begin=j+1;
        			if(length>i-j){
        				length=i-j;
        				minBegin=j;
        			}
        			break;
        		}
        	}
        }
        return length!=s.length()+1?s.substring(minBegin, minBegin+length+1):"";
    }
时间: 2024-10-11 17:20:16

Java for LeetCode 076 Minimum Window Substring的相关文章

【leetcode】Minimum Window Substring

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

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 76 Minimum Window Substring ----- 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 window i

Leetcode 76 Minimum Window Substring. (最小窗口子字符串) (滑动窗口, 双指针)

目录 问题描述 例子 解决方案 ** Leetcode 76 ** 问题描述 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&qu

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 in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". Note: If there is no such windo

leetCode(55):Minimum Window Substring(limits.h头文件)

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 windo

【leetcode】Minimum Window Substring (hard) ★

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[76] Minimum Window Substring

给定两个串,S和T,在S中找到包含T的最短子串,如果不能包含,返回空字符. 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&

LeetCode – Refresh – Minimum Window Substring

Notes: 1. When check left shifting, do not use continue, but break it!!! Otherwise, you wil fall into infinite loop 2. Initialize the map with T, not S!!!! 1 class Solution { 2 public: 3 string minWindow(string S, string T) { 4 int len = S.size(), co