Java Concise


  • 0
    V
    public class Solution {
        public boolean checkInclusion(String s1, String s2) {
            int[] need = new int[256];
            for (char c : s1.toCharArray()) need[c]++;
            
            int counter = s1.length(), lo = 0;
            char[] A = s2.toCharArray();
            boolean found = s1.length() == 0;
            for(int hi = 0; hi < A.length && !found; hi++) {
                need[A[hi]]--;
                if (need[A[hi]] >= 0) counter--;
                else {
                    while(need[A[hi]] < 0) {
                        need[A[lo]]++;
                        if (need[A[lo]] > 0) counter++;
                        lo++;
                    }
                }
                found = counter == 0; 
            }
    
            return found;
        }
    }
    

Log in to reply
 

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