Valid Anagram

Problem Summary

(Write in your own words, not copied from LeetCode. This forces comprehension.)

What is Being Asked?

(One sentence on the actual task.)

Key Observations

(Patterns, constraints, or hints in the problem statement.)

Approach Taken

(Step-by-step logic or pseudocode before coding.)

Why This Works

(Explain the core reason the solution is correct.)

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        letter_to_count_in_s = {} # a: 3, n: 1, g: 1, r: 1, m: 1
        letter_to_count_in_t = {} # n: 1, a: 3, g: 1, r: 1, m: 1

        if len(s) != len(t):
            return False

        # s = anagram
        # t = nagaram
        # a,n
        for letter_in_s, letter_in_t in zip(s, t):
            letter_to_count_in_s[letter_in_s] = letter_to_count_in_s.get(letter_in_s, 0) + 1
            letter_to_count_in_t[letter_in_t] = letter_to_count_in_t.get(letter_in_t, 0) + 1

        return letter_to_count_in_s == letter_to_count_in_t
    # time: O(n)
    # space: O(1)