LeetCode Q 242 - Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s.
Similar question: Group Anagrams
Solution
Code:
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
int[] alphabet = new int[26];
for (char ch: s.toCharArray()) alphabet[ch - 'a']++;
for (char ch: t.toCharArray()) alphabet[ch - 'a']--;
for (int i = 0; i <26; i++) {
if (alphabet[i] != 0) return false;
}
return true;
}