更新时间:2022-10-02 19:24:04
242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
class Solution {
public :
bool isAnagram(string s, string t) {
if (s.size() != t.size())
{
return false ;
}
else
{
int sBit[26] = {0}; //记录每个字母出现的次数
int tBit[26] = {0};
const char * sp = s.c_str();
const char * tp = t.c_str();
for ( int i = 0; i < s.size() ; i++)
{
sBit[*(sp+i) - 'a' ]++;
tBit[*(tp+i) - 'a' ]++;
}
for ( int j = 0; j < 26;j++)
{
if (sBit[j] != tBit[j])
{
return false ;
}
}
return true ;
}
}
}; |
本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1834716