3 ms, standard two pointer, easy JAVA solution


  • 1
    Z
    public class Solution {
        public String minWindow(String s, String t) {
            int[] map = new int[128];
            for (char ch: t.toCharArray()) map[ch]++;
            
            int count = t.length(), leftPointer = 0, rightPointer = 0, head = 0, minLen = Integer.MAX_VALUE;
            
            while (rightPointer < s.length()) {
                if (map[s.charAt(rightPointer++)]-- > 0)  
                    count--;
                while (count == 0) {
                    if (rightPointer - leftPointer < minLen) {
                        head = leftPointer;
                        minLen = rightPointer - leftPointer;
                    }
                    if (map[s.charAt(leftPointer++)]++ == 0) 
                        count++;
                }
            }
            
            if (minLen == Integer.MAX_VALUE) return "";
            return s.substring(head, head + minLen);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.