您的位置:首页 > 其它

leetcode-Valid Anagram

2015-11-05 23:24 344 查看
Difficulty:
Easy

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) {
map<char,int> check;
int nonZero=0;
for(auto &e:s)
if(++check[e]==1)
++nonZero;
for(auto &e:t){
if(--check[e]==0)
--nonZero;
else if(check[e]==-1)
++nonZero;
}
return nonZero==0;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: