Scribbling

[C++] LeetCode 242. Valid Anagram 본문

Computer Science/C++

[C++] LeetCode 242. Valid Anagram

focalpoint 2023. 8. 18. 05:15

https://leetcode.com/problems/valid-anagram/

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.size() != t.size()) {
            return false;
        }
        unordered_map<char, int> map1;
        unordered_map<char, int> map2;
        for (auto c : s) {
            map1[c]++;
        }
        for (auto c : t) {
            map2[c]++;
        }
        for (auto x : map1) {
            if (x.second != map2[x.first]) {
                return false;
            }
        }
        return true;
    }
};