LeetCode – 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".

Java Solution

public String minWindow(String s, String t) {
        return "";
    String result = "";
    //character counter for t
    HashMap<Character, Integer> target = new HashMap<Character, Integer>();
    for(int i=0; i<t.length(); i++){
        char c = t.charAt(i);    
    // character counter for s
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    int left = 0;
    int minLen = s.length()+1;
    int count = 0; // the total of mapped characters
    for(int i=0; i<s.length(); i++){
        char c = s.charAt(i);
        if(count == t.length()){
            char sc = s.charAt(left);
            while (!map.containsKey(sc) || map.get(sc) > target.get(sc)) {
                if (map.containsKey(sc) && map.get(sc) > target.get(sc))
                    map.put(sc, map.get(sc) - 1);
                sc = s.charAt(left);
            if (i - left + 1 < minLen) {
                result = s.substring(left, i + 1);
                minLen = i - left + 1;
    return result;
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Larry Okeke

    This runs in O(n log n). but the idea is easier to understand.

    count through word till you’ve gotten all the characters in your target substring, and then compare the length to last result.

    import java.util.*;

    public class shortest_window{

    public static void main(String[] args){

    String word = "ADOBECODEBANC";

    String target = "ABC";

    System.out.println(solution(word, target));


    public static String solution(String word, String targ){

    if(word==null || word.length() < 1 ) return "";

    if(targ==null || targ.length() < 1) return "";

    char arr = new targ.toCharArray();

    String shortest = word;

    for(int i =0; i < word.length(); i++){

    String temp = "";

    ArrayList target = new ArrayList();

    for(Character c: arr){



    for(int j = i; j temp.length()) shortest = temp;





    return shortest;



  • Renzo
  • gao can

    No, it doesn’t need to do it. Although it is a good idea to decrease count by 1. Then taking one character out of the map and increase left by 1.
    Think about this case, first we find “ADOBEC” then when count == t.length(), left is A since we can not drop any of character then we move left to D and move A from map and count–. This is the method that you talk about.
    But we can keep count as 3 and without moving left, we still move i to next A,
    then it becomes “ADOBECODEBA”, here count == 3 == t.length(), but now we can move left from the first A to C, it becomes “CODEBA”.
    See, that is the reason we don’t need to decrease count by 1.

  • Kyle

    You’re right. 🙂

  • Nand Jha

    count == t.length()
    After doing the operation when you meet your desired count of characters in T, you need to decrease count by 1 as you are removing character one by one. AT the end when it will find the desired smallest length it would take one character out of the map and hence it should decrease that count by 1 as well.