leetcode-Valid Anagram

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;        
    }
};


你可能感兴趣的:(leetcode-Valid Anagram)