242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.
For 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?
此题比较简单,甚至可以不用使用hashtable来做,只要创建一个数组,然后记录字符串里面字符出现的个数,代码如下:
1 public class Solution { 2 public boolean isAnagram(String s, String t) { 3 int[] word = new int[26]; 4 for(char c:s.toCharArray()){ 5 word[c-'a']++; 6 } 7 for(char c:t.toCharArray()){ 8 word[c-'a']--; 9 } 10 for(int i:word){ 11 if(i!=0) return false; 12 } 13 return true; 14 } 15 }