LeetCode 242 Valid Anagram

Tag: LeetCode Posted on 2022-03-06 11:20:20 Edited on 2022-03-06 11:20:20 Views: 130

概述

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

单哈希表法

class Solution {
public:
    bool isAnagram(string s, string t) {
        unordered_map<char, int> m;
        if (s.size() != t.size()) return false;
        for (int i = 0; i < s.size(); i ++) {
            m[s[i]] ++;
            m[t[i]] --;
            if (m[s[i]] == 0) m.erase(s[i]);
            if (m[t[i]] == 0) m.erase(t[i]);
        }
        return m.empty();
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/