Valid Anagram

Table of Contents

Problem: Valid Anagram

Thoughts

Multiple ways to do this one. The fastest should be O(n) time and O(n) space.

Use a hash map to count the appearances of each item and if they’re the same by the end then they’re anagrams.

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        from collections import Counter

        c1 = Counter(s)
        c2 = Counter(t)

        return c1 == c2

Troy Fischer

2026-05-25 Mon 14:19