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 in S that covers all characters in T, return the empty string "".

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

思路:举个例子吧

S="aabcdafac"   T="ac"

保存两个指针,分别指向当前已找到的字符串的start和end位置;我们先找到了aabc,这是第一个包含ac的字符串,好的,这个长度为4,start=0,end=3,这时,保持end不动,移动start,发现start=1,end=3可以更短。之后再就不能移start了。

接下来,咱们移动end,发现移到end=4,不是a或c,那就再移,移到end=5,发现是a,这时start还是1,咱们移动start,发现可以一直移到start=3。

接下来再移动end,如此类推

结合代码走一走,就会发现这个思路还是很线性的。

package string;

import java.util.HashMap;
import java.util.Map;

public class MinimumWindowSubstring {

    public String minWindow(String s, String t) {
        int m = s.length();
        int n = t.length();
        Map<Character, Integer> found = new HashMap<Character, Integer>();
        Map<Character, Integer> base = new HashMap<Character, Integer>();
        for (int i = 0; i < n; ++i) {
            char c = t.charAt(i);
            if (base.containsKey(c)) {
                base.put(c, base.get(c) + 1);
            } else {
                base.put(c, 1);
                found.put(c, 0);
            }
        }
        
        int count = 0;
        int ss = -1;
        int ee = m;
        for (int end = 0, start = 0; end < m; ++end) {
            char c = s.charAt(end);
            if (base.containsKey(c)) {
                found.put(c, found.get(c) + 1);
                if (found.get(c) <= base.get(c))
                    ++count;
                if (count == n) {
                    char startChar = s.charAt(start);
                    while (!base.containsKey(startChar) || found.get(startChar) > base.get(startChar)) {
                        if (base.containsKey(startChar))
                            found.put(startChar, found.get(startChar) - 1);
                        startChar = s.charAt(++start);
                    }
                    
                    if (end - start < ee - ss) {
                        ee = end;
                        ss = start;
                    }
                }
            }
        }
        
        return ss == -1 ? "" : s.substring(ss, ee + 1);
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String S = "ADOBECODEBANC";
        String T = "ABC";
        MinimumWindowSubstring m = new MinimumWindowSubstring();
        System.out.println(m.minWindow(S, T));
    }

}

 

转载于:https://www.cnblogs.com/null00/p/5094630.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值