给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram"
输出: true
示例 2:
输入: s = "rat", t = "car"
输出: false
说明:
你可以假设字符串只包含小写字母。
进阶:
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
class Solution { public boolean isAnagram_1(String s, String t) { char[] sChars = s.toCharArray(); char[] tChars = t.toCharArray();
Arrays.sort(sChars);
Arrays.sort(tChars); return String.valueOf(sChars).equals(String.valueOf(tChars));
} public boolean isAnagram_2(String s, String t) {
Map<Character, Integer> map = new HashMap<>(); for (char ch : s.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
} for (char ch : t.toCharArray()) {
Integer count = map.get(ch); if (count == null) { return false;
} else if (count > 1) {
map.put(ch, count - 1);
} else {
map.remove(ch);
}
} return map.isEmpty();
} public boolean isAnagram(String s, String t) { int[] sCounts = new int[26]; int[] tCounts = new int[26]; for (char ch : s.toCharArray()) {
sCounts[ch - 'a']++;
} for (char ch : t.toCharArray()) {
tCounts[ch - 'a']++;
} for (int i = 0; i < 26; i++) { if (sCounts[i] != tCounts[i]) { return false;
}
} return true;
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69927547/viewspace-2648590/ ,如需转载,请注明出处,否则将追究法律责任。