Question
Given two strings s and t, write a function to determine if t is an anagram of s.
Example
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.
Note
You may assume the string contains only lowercase alphabets.
Follow up
What if the inputs contain unicode characters? How would you adapt your solution to such case?
Java Code
//使用数组记录字符串中各个字符出现的次数,数组的索引对应字符的ASCII码
public boolean isAnagram(String s, String t) {
int len = s.length();
if(len != t.length()) return false;
int[] sTable = new int[128];//默认初始化为0
int[] tTable = new int[128];
for(int i = 0; i < len; ++i) {
sTable[s.charAt(i)]++;
tTable[t.charAt(i)]++;
}
for(int i = 0; i < len; ++i) {
if(sTable[i] != tTable[i])
return false;
}
return true;
}