O(n) Java solution


  • 2
    W
     public boolean isAnagram(String s, String t) {
        if(s == null || t == null || s.length() != t.length()){
            return false;
        }
        
        int[] arr = new int[26];
        
        for(char c : s.toCharArray()){
            arr[c - 'a']++;
        }
        
        for(char c : t.toCharArray()){
            if(--arr[c - 'a'] < 0 ){
                return false;
            }
        }
        
        return true;
    }

Log in to reply
 

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