给定两个字符串 s
和 t
,编写一个函数来判断 t
是否是 s
的字母异位词。
注意:若 s
和 t
中每个字符出现的次数都相同,则称 s
和 t
互为字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram" 输出: true
示例 2:
输入: s = "rat", t = "car" 输出: false
提示:
1 <= s.length, t.length <= 5 * 104
s
和t
仅包含小写字母
进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
数组或哈希表累加 s 中每个字符出现的次数,再减去 t 中对应的每个字符出现的次数。遍历结束后,若字符中出现次数不为 0 的情况,返回 false,否则返回 true。
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
chars = [0] * 26
for i in range(len(s)):
chars[ord(s[i]) - ord('a')] += 1
chars[ord(t[i]) - ord('a')] -= 1
return all(c == 0 for c in chars)
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] chars = new int[26];
for (int i = 0; i < s.length(); ++i) {
++chars[s.charAt(i) - 'a'];
--chars[t.charAt(i) - 'a'];
}
for (int c : chars) {
if (c != 0) {
return false;
}
}
return true;
}
}
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size())
return false;
vector<int> chars(26, 0);
for (int i = 0, n = s.size(); i < n; ++i) {
++chars[s[i] - 'a'];
--chars[t[i] - 'a'];
}
for (int c : chars) {
if (c != 0)
return false;
}
return true;
}
};
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
var chars [26]int
for i := 0; i < len(s); i++ {
chars[s[i]-'a']++
chars[t[i]-'a']--
}
for _, c := range chars {
if c != 0 {
return false
}
}
return true
}
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length != t.length) return false;
let record = new Array(26).fill(0);
let base = 'a'.charCodeAt(0);
for (let i = 0; i < s.length; ++i) {
++record[s.charCodeAt(i) - base];
--record[t.charCodeAt(i) - base];
}
return record.every(v => v == 0);
};
function isAnagram(s: string, t: string): boolean {
const n = s.length;
const m = t.length;
return n === m && [...s].sort().join('') === [...t].sort().join('');
}
function isAnagram(s: string, t: string): boolean {
const n = s.length;
const m = t.length;
if (n !== m) {
return false;
}
const count = new Array(26).fill(0);
for (let i = 0; i < n; i++) {
count[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
count[t.charCodeAt(i) - 'a'.charCodeAt(0)]--;
}
return count.every(v => v === 0);
}
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
let n = s.len();
let m = t.len();
if n != m {
return false;
}
let mut s = s.chars().collect::<Vec<char>>();
let mut t = t.chars().collect::<Vec<char>>();
s.sort();
t.sort();
for i in 0..n {
if s[i] != t[i] {
return false;
}
}
true
}
}
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
let n = s.len();
let m = t.len();
if n != m {
return false;
}
let (s, t) = (s.as_bytes(), t.as_bytes());
let mut count = [0; 26];
for i in 0..n {
count[(s[i] - b'a') as usize] += 1;
count[(t[i] - b'a') as usize] -= 1;
}
count.iter().all(|&c| c == 0)
}
}
int cmp(const void *a, const void *b) {
return *(char *) a - *(char *) b;
}
bool isAnagram(char *s, char *t) {
int n = strlen(s);
int m = strlen(t);
if (n != m) {
return 0;
}
qsort(s, n, sizeof(char), cmp);
qsort(t, n, sizeof(char), cmp);
return !strcmp(s, t);
}
bool isAnagram(char *s, char *t) {
int n = strlen(s);
int m = strlen(t);
if (n != m) {
return 0;
}
int count[26] = {0};
for (int i = 0; i < n; i++) {
count[s[i] - 'a']++;
count[t[i] - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (count[i]) {
return 0;
}
}
return 1;
}