LeetCode 125 Valid Palindrome - Example and Complexity Analysis

Walkthrough and analysis

Walk through s = "A man, a plan, a canal: Panama".

Initialize: left = 0 (points to 'A'), right = 29 (points to 'a').

Compare 'A' and 'a' (case-insensitive): match. Move both pointers.

left = 1 is space. Skip. left = 2 is 'm'. right = 28 is 'm'. Compare 'm' and 'm': match.

Continue this process. Each pointer moves inward, skipping non-alphanumeric characters. All comparisons match until the pointers cross.

Each character is visited at most once by each pointer. That's O(n)O(n) time.

No extra storage needed beyond two index variables. That's O(1)O(1) space.