242.有效的字母异位词
class Solution {
public boolean isAnagram(String s, String t) {
int[] count1 = encode(s);
int[] count2 = encode(t);
// 确保两个字符串中所有字符出现的数量相同
for (int i = 0; i < count1.length; i++) {
if (count1[i] != count2[i]) {
return false;
}
}
return true;
}
// 计算字符的出现次数
public int[] encode(String s) {
int[] result = new int[26];
char[] arr = s.toCharArray();
for (char c : arr) {
int delta = c - 'a';
result[delta]++;
}
return result;
}
}