242. Valid Anagram

https://leetcode.com/problems/valid-anagram/

O(n),O(1)

class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;
        
        int a[] = new int[26];
        for (char c : s.toCharArray()) {
            a[c - 'a']++;
        }
        for (char c : t.toCharArray()) {
            if (--a[c - 'a'] < 0) {
                return false;
            }
        }

        return true;
    }
}

Last updated