LeetCode-242-有效的字母异位词

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

示例 1:

1
2
输入: s = "anagram", t = "nagaram"
输出: true

示例 2:

1
2
输入: s = "rat", t = "car"
输出: false

说明:
你可以假设字符串只包含小写字母。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size()!=t.size())
return false;
//用来记录字符出现的次数
int count[26]={0};
for(int i=0;i<s.size();i++){
count[s[i]-97]++;
count[t[i]-97]--;
}
for(int i=0;i<26;i++){
if(count[i]!=0)
return false;
}

return true;
}
};

----\(˙<>˙)/----赞赏一下吧~