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.
解法:英文颠倒词,只要两个单词中,英文字母出现的次数相等即可。
代码如下:
public class Solution { public boolean isAnagram(String s, String t) { int []map=new int[26]; char[] c1=s.toCharArray(); for(char t1:c1){ map[t1-'a']++; } char[]c2=t.toCharArray(); for(char t2:c2){ map[t2-'a']--; } for(int i=0;i<26;i++){ if(map[i]!=0) return false; } return true; } }
运行结果: