LeetCode: Minimum Window Substring

來源:互聯網
上載者:User

標籤:style   class   blog   c   code   java   

 1 /** 2  *  3  */ 4 package solution; 5  6 import java.util.HashMap; 7  8 /** 9  * @author whh10  * 11  *         Given a string S and a string T, find the minimum window in S which12  *         will contain all the characters in T in complexity O(n).13  * 14  *         For example, S = "ADOBECODEBANC", T = "ABC" Minimum window is "BANC".15  * 16  *         Note:17  * 18  *         If there is no such window in S that covers all characters in T,19  *         return the emtpy string "". If there are multiple such windows, you20  *         are guaranteed that there will always be only one unique minimum21  *         window in S.22  */23 public class MinimumWindowSubstring {24 25     /**26      * @param args27      */28     public static void main(String[] args) {29         MinimumWindowSubstring mws = new MinimumWindowSubstring();30         String S = "aabbDEE", T = "aabbDEE";31         System.out.println(mws.minWindow(S, T));32     }33 34     /**35      * @param S36      * @param T37      * @return38      */39     public String minWindow(String S, String T) {40 41         HashMap<Character, Integer> hasFound = new HashMap<Character, Integer>();42         HashMap<Character, Integer> needToFind = new HashMap<Character, Integer>();43 44         for (int i = 0; i < T.length(); i++) {45             hasFound.put(T.charAt(i), 0);46 47             if (needToFind.containsKey(T.charAt(i))) {48                 needToFind.put(T.charAt(i), needToFind.get(T.charAt(i)) + 1);49             } else {50                 needToFind.put(T.charAt(i), 1);51             }52         }53 54         int begin = 0;55         int minWindowSize = S.length();56         String retString = "";57 58         int count = 0;59 60         for (int end = 0; end < S.length(); end++) {61             Character end_c = S.charAt(end);62             if (needToFind.containsKey(end_c)) {63                 hasFound.put(end_c, hasFound.get(end_c) + 1);64                 if (hasFound.get(end_c) <= needToFind.get(end_c)) {65                     count++;66                 }67                 if (count == T.length()) {68                     while ((!needToFind.containsKey(S.charAt(begin)))69                             || (hasFound.get(S.charAt(begin)) > needToFind70                                     .get(S.charAt(begin)))) {71 72                         if (needToFind.containsKey(S.charAt(begin))) {73                             hasFound.put(S.charAt(begin),74                                     hasFound.get(S.charAt(begin)) - 1);75                         }76 77                         begin++;78                     }79 80                     if ((end - begin + 1) <= minWindowSize) {81                         minWindowSize = end - begin + 1;82                         retString = S.substring(begin, end + 1);83                     }84                 }85             }86         }87 88         return retString;89     }90 91 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.