242. Valid Anagram

来源:互联网 发布:软件业务销售合同范本 编辑:程序博客网 时间:2024/06/14 17:22

题目:

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.

思路:

本题求有效哈希表,充分利用unorder_map即可

代码:

class Solution {public:    bool isAnagram(string s, string t) {        int n = s.size();        if(n!=t.size())              return false;          unordered_map<char, int> mp;        for(int i;i<n;i++)        {            mp[s[i]]++;            mp[t[i]]--;        }        for(auto count:mp)            if(count.second)                return false;        return true;            }};


原创粉丝点击