`

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

 

Solution:

bool isAnagram(string s, string t) {
    if(s.size() != t.size()) return false;
    unordered_map<char, int> map;
    for(auto c:s) map[c]++;
    for(auto c:t) {
        if(--map[c]<0) return false;
    }
    return true;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics