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.
思路:
给定两个字符串,字符串的大小一样,只有顺序不一样。
两个字符串只包含小写字母。
class Solution {
public:
bool isAnagram(string s, string t) {
int len_s = s.length();
int len_t = t.length();
if(len_s != len_t){
return false;
}
vector<int> count(26, 0);
for(int i =0; i < len_s; i++){
count[s[i] - 'a']++;
}
for(int i = 0; i < len_t; i++){
if( --count[t[i]-'a']<0 )
{
return false;
}
}
return true;
}
};
本博客介绍了一个函数,用于确定给定的两个字符串是否为异位词。两个字符串仅包含小写英文字母,并且大小相同,只是顺序不同。
662

被折叠的 条评论
为什么被折叠?



