all entries
A-010Day 102026.05.12easyleetcode #125neetcode150

Valid Palindrome

#two-pointers#string
leetcode #125 · valid-palindrome
01

Problem

· problem
P.010

Valid Palindrome

leetcode #125

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise.

constraints
  • · 1 ≤ s.length ≤ 2 × 10⁵
  • · s consists only of printable ASCII characters
// paraphrased summary — see source for full text
examples
example 1input → output
"A man, a plan, a canal: Panama"
true
example 2input → output
"race a car"
false
example 3input → output
" "
true
02

Pre-solve

· pre-solve
● 1list shown● 2select● 3reveal
  • What should we do with uppercase letters?
  • Which characters should we keep?
  • How do we check if a string is a palindrome?
  • What if the input is empty or contains only non-alphanumeric characters?
  • Should we remove spaces but keep punctuation?
  • Do we check if the original string is a palindrome as-is?
  • Can we use extra space to store a cleaned version?
check the items you would ask, then press confirm
// session-only state — refresh resets (repeatable practice)
03

Logic Structure

· logic
● 1slots shown● 2pick per slot● 3reveal
// pick one code line per slot to assemble the algorithm flow. no typing — just the logic skeleton.
step 1· Initialize cleaned string
new = ''
new = []
new = None
cleaned = ''
step 2· Iterate through input string
for a in s:
for i in range(len(s)):
for a in s.lower():
step 3· Filter alphanumeric charactersnested
if a.isalpha() or a.isdigit():
if not a.isspace():
if a.isupper() or a.isdigit():
if a != ' ' and a != ',':
step 4· Lowercase and accumulate│ │ nested
new += a.lower()
new += a
new.append(a.lower())
new = new + a.upper()
step 5· Compare with reverse
return (new == new[::-1])
return (new == reversed(new))
return (s == s[::-1])
return len(new) == 0
pick one option per slot
// format: slot — recursive / DP patterns use ordering / state-first formats. ADR-08 follow-up.
04

Solve · Trace

· solve
solution.py
1
class Solution:
2
    def isPalindrome(self, s: str) -> bool:
3
        new = ''
4
        for a in s:
5
            if a.isalpha() or a.isdigit():
6
                new += a.lower()
7
        return (new == new[::-1])
mental dry-run cases
// walk each case in your head; expand the worked example below if stuck.
case 1
"A man, a plan, a canal: Panama"
true
case 2
"race a car"
false
case 3
" "
true
// UI does not walk-through — you do the dry-run mentally. Expand the worked example if stuck.