🙄

100日アルゴリズム[9日目・hashTable]

2024/03/28に公開

解いた問題

https://leetcode.com/problems/valid-anagram/
アナグラムの問題。

回答

function isAnagram(s: string, t: string): boolean {
    let countS: Map<string, number> = new Map;
    let countT: Map<string, number> = new Map;
    for (const count of s) {
        countS.set(count, (countS.get(count) || 0) + 1)
    }

    for (const count of t) {
        countT.set(count, (countT.get(count) || 0) + 1)
    }

    if (countS.size !== countT.size) {
        return false;
    }

    for (const [key, value] of countS) {
        if (countT.get(key) !== value) return false;
    }
    return true;
};

Discussion