Java solution using array (beats 60%?)


  • 0
    A

    A java solution:

    public boolean isAnagram(String s, String t) {
        int[] array = new int[26];
    
        for (int i = 0; i < s.length(); i++) {
            array[s.charAt(i) - 'a']++;
        }
    
        for (int i = 0; i < t.length(); i++) {
            --array[t.charAt(i) - 'a'];
        }
    
        for (int anArray : array) {
            if (anArray != 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.