Valid Anagram

easyTests passed 2026-11-11v1.0.0

1. Core Invariant / The Trick

Two strings are anagrams iff they have equal length and identical character frequency histograms. Increment for `s` and decrement for `t` in one 26-slot array (or map) and verify all counts return to 0.

2. Complexity Bounds & Proof

Fixed alphabet (26 lowercase letters) bounds auxiliary frequency array to O(1); time is linear in string length N.

3. Minimal Idiomatic Code

def isAnagram(s: str, t: str) -> bool:
    if len(s) != len(t):
        return False
    counts = [0] * 26
    for cs, ct in zip(s, t):
        counts[ord(cs) - 97] += 1
        counts[ord(ct) - 97] -= 1
    return all(c == 0 for c in counts)

4. Failure Modes & Interview Follow-ups

Pitfalls:- Failing fast on `len(s) != len(t)` saves work. - Unicode follow-ups require a hash map instead of a fixed 26-int array.
Follow-ups:- Group Anagrams: map each string to its sorted signature or 26-tuple frequency key.

Personal Notes & Scratchpad (Local-First Yjs CRDT)

Saved instantaneously in IndexedDB. Streams live across peers via Hocuspocus when signed in.

Related Invariants

Browser Scratchpad
Console Output
Click Run or press Cmd/Ctrl+Enter to execute locally in a Web Worker.