share my Java solution, beating 98%!


  • 0
    T
    public class Solution {
        public boolean checkInclusion(String s1, String s2) {
              char[] array1=s1.toCharArray();
              int[] hash=new int[26];
              for(int i=0;i<array1.length;i++){
                  hash[array1[i]-'a']++;
              }
              
              char[] array2=s2.toCharArray();
              int start=0;
              
              for(int i=0;i<array2.length;i++){
                  hash[array2[i]-'a']--;
                  
                  while(hash[array2[i]-'a']<0){
                      hash[array2[start]-'a']++;
                      start++;
                  }
                  
                  if(i-start+1==array1.length){
                      return true;
                  }
                  
              }
              
              return false;
        }
    }
    
    

Log in to reply
 

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