Clicky

Leetcode 242 "Valid Anagram"

January 1, 0001

242 Valid Anagram

Runtime: 5 ms, faster than 49.66% of Java online submissions for Valid Anagram. Memory Usage: 39.5 MB, less than 57.91% of Java online submissions for Valid Anagram.

class Solution {
    public boolean isAnagram(String s, String t) {
        int[] arr1 = new int[27];
        int[] arr2 = new int[27];
        
        if(s.length() != t.length())
            return false;
        
        System.out.println((int)s.charAt(0));
        for(int i=0; i<s.length(); i++) {
            arr1[(int)s.charAt(i) -97] = arr1[(int)s.charAt(i) -97] + 1;
        }
        for(int i=0; i<t.length(); i++) {
            arr2[(int)t.charAt(i) -97] = arr2[(int)t.charAt(i) -97] + 1;
        }
        for(int i=0; i<arr1.length; i++) {
           if(arr1[i] != arr2[i])
               return false;
        }
        return true;
    }
}