https://leetcode-cn.com/problems/valid-anagram/
- 自己的解(哈希表)
class Solution {
public boolean isAnagram(String s, String t) {
char[] arrS = s.toCharArray();
char[] arrT = t.toCharArray();
int[] numS = new int[100];
int[] numT = new int[100];
for(char c:arrS){
numS[c-'0']++;
}
for(char c:arrT){
numT[c-'0']++;
}
for(int i=0;i<numS.length;i++){
if(numS[i]!=numT[i]){
return false;
}
}
return true;
}
}
- 排序
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length())return false;
char[] s1 = s.toCharArray();
char[] t1 = t.toCharArray();
Arrays.sort(s1);
Arrays.sort(t1);
return Arrays.equals(s1,t1);
}
}