76. Minimum Window Substring

44 篇文章 0 订阅
13 篇文章 0 订阅

Problem:

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"

Note:

  • If there is no such window in S that covers all characters in T, return the empty string "".
  • If there is such window, you are guaranteed that there will always be only one unique minimum window in S.

Analysis:

本题的思路是维持一个HashMap,每次与T字符串进行匹配,看是否满足,然后找出长度最短的那个。代码如下:

Code:

class Solution {
    public String minWindow(String s, String t) {
        if(s.length() == 0 || t.length() == 0  || s.length() < t.length())
            return "";
        
        int minLen = s.length() + 1; // save minimum string length
        int fir = -1, lat = -1;
        int pre = 0, nxt = 0; // two pointer to traversal string
        Map<Character, Integer> tChar = new HashMap<Character, Integer>(); // store string(t) character number
        Map<Character, Integer> temp = new HashMap<Character, Integer>(); // cache every substring character number
        
        for(int i = 0; i < t.length(); i++) {
            char ch = t.charAt(i);
            if(!tChar.containsKey(ch)) {
                tChar.put(ch, 1);
            } else {
                tChar.put(ch, tChar.get(ch) + 1);
            }
        }
                          
        // traversal the string array
        while(pre <= nxt && nxt < s.length()) {
            
            char ch = s.charAt(nxt);
            if(!temp.containsKey(ch)) {
                temp.put(ch, 1);
            } else {
                temp.put(ch, temp.get(ch) + 1);
            }
            
            if(isWindow(tChar, temp)) {
                while(pre <= nxt)
                {
                    char pch = s.charAt(pre);
                    if(!tChar.containsKey(pch)) {
                        temp.remove(pch);
                        pre++;
                    } else if(tChar.get(pch) < temp.get(pch)) {
                        temp.put(pch, temp.get(pch) - 1);
                        pre++;
                    } else {
                        break;
                    }                      
                }
                if(minLen > nxt - pre + 1) {
                    minLen = nxt - pre + 1;
                    fir = pre;
                    lat = nxt;
                }
            }
            nxt++;
        }
        
        // System.out.println(minLen + "=" + fir + ", " + lat);
        return (minLen == s.length() + 1) ? "" : (fir == -1 ? "" :s.substring(fir, lat + 1));
        
    }
    
    public boolean isWindow(Map<Character, Integer> t, Map<Character, Integer> s) {
        for(Map.Entry<Character, Integer> entry : t.entrySet()) {
            Character keyStr = entry.getKey();
            int val = entry.getValue();
            if(!s.containsKey(keyStr) || s.get(keyStr) < val)
                return false;
        }
        return true;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值