LeetCode--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.

代码:

    bool isAnagram(string s, string t) {
        unordered_map<char, int> strS;
        unordered_map<char, int> strT;
        for(int i = 0; i < s.length(); ++i)
        {
            strS[s[i]]++;
        }
         for(int i = 0; i < t.length(); ++i)
        {
            strT[t[i]]++;
        }
        if(strS == strT)
            return true;
        else
            return false;
    }
用unordered_map分别统计每个字母,再比较

posted on 2015-11-03 13:40  小二杰  阅读(97)  评论(0编辑  收藏  举报

导航