Valid Anagram

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;
}

   Reprint policy


《Valid Anagram》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC