tag:
- sort
- hashtable
验证相同字母异序词
排序,比较 O(nlogn)
哈希表 O(n)
java
//sort
// public boolean isAnagram(String s, String t) {
// if(s==null||t==null||s.length()!=t.length()) return false;
// char[] ss = s.toCharArray();
// char[] tt = t.toCharArray();
// Arrays.sort(ss);
// Arrays.sort(tt);
// if(new String(ss).equals(new String(tt))) return true;
// return false;
// }
//map
public boolean isAnagram(String s, String t) {
int[] map = new int[26];
for(int i=0; i<s.length(); i++) {
map[s.charAt(i)-'a']++;
}
for(int i=0; i<t.length(); i++) {
map[t.charAt(i)-'a']--;
}
for(int m : map){
if(m!=0) return false;
}
return true;
}
go