Java Array Easy Method


  • 0
    C

    public class Solution {
    public String minWindow(String s, String t) {
    if (s.length() < t.length()) return "";
    Integer minimum = Integer.MAX_VALUE;

        int[] hash = new int[256];
        for (char c : t.toCharArray()) hash[c]++;
        int leftPoint = 0, rightPoint = 0, count = t.length();
        int resRight = 0, resLeft = 0;
    
        while (rightPoint < s.length()) {
            char cRight = s.charAt(rightPoint);
            hash[cRight]--;
            if (hash[cRight] >= 0) count--;
            rightPoint++;
            while (count == 0) {
                if (rightPoint - leftPoint < minimum) {
                    minimum = rightPoint - leftPoint;
                    resRight = rightPoint; resLeft = leftPoint;
                }
    
                char cLeft = s.charAt(leftPoint);
                hash[cLeft]++;
                if (hash[cLeft] > 0) count++;
                leftPoint++;
            }
        }
        return minimum == Integer.MAX_VALUE ? "" : s.substring(resLeft, resRight);        
    }
    

    }


Log in to reply
 

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