Valid Palindrome

easyTests passed 2026-11-11v1.0.0

1. Core Invariant / The Trick

Converging left/right pointers skipping non-alphanumeric characters, comparing lowercased chars in O(1) extra space without allocating filtered strings.

2. Complexity Bounds & Proof

Left and right pointers advance strictly toward each other across at most N characters.

3. Minimal Idiomatic Code

def isPalindrome(s: str) -> bool:
    l, r = 0, len(s) - 1
    while l < r:
        while l < r and not s[l].isalnum(): l += 1
        while l < r and not s[r].isalnum(): r -= 1
        if s[l].lower() != s[r].lower(): return False
        l, r = l + 1, r - 1
    return True

4. Failure Modes & Interview Follow-ups

Pitfalls:- Allocating a reversed cleaned string wastes O(N) memory.
Follow-ups:- Valid Palindrome II (allow deleting 1 char): on mismatch, check `isPal(l+1, r) || isPal(l, r-1)`.

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.