Valid Anagram
给两个字符串s和t, 看看是不是互为重组.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class Solution { public boolean isAnagram(String s, String t) { int[] s_count = new int[256]; int[] t_count = new int[256]; for(int i = 0 ; i < s.length(); i++) s_count[s.charAt(i)]++; for(int i = 0 ; i < t.length(); i++) t_count[t.charAt(i)]++; return hash(s_count) == hash(t_count); } public int hash(int[] count){ int a = 3; int b = 7; int hash = 0; for(int i = 0 ; i < count.length; i++){ hash += count[i]*a + b; a*=b; } return hash; } } |