LeetCode,leetcodeoj

來源:互聯網
上載者:User

LeetCode,leetcodeoj

題目:

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));    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.