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.

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size()!=t.size())
            return false;
            
        unordered_map<char,int> sHash;
        for(auto sch:s)
            ++sHash[sch];
        
        for(auto tch :t){
            if(--sHash[tch] < 0)
                return false;
        }
        
        int res = 0;
        for(auto maptmp:sHash){
            res+=maptmp.second;
        }
        if(!res)
            return true;
        else 
            return false;
    }
};

 

posted on 2017-03-05 04:28  123_123  阅读(97)  评论(0编辑  收藏  举报