Skip to content
Veltoryx Technologies
FreeProblem 16● EasyLeetCode #125 ↗3 min readUpdated 30 October 2026

Valid Palindrome

Check whether a phrase reads the same both ways, ignoring case and symbols, with one pointer at each end.

01The problem

A phrase is a palindrome if, after lower-casing it and ignoring everything that isn't a letter or digit, it reads the same forwards and backwards. Return whether s is a palindrome.

InputOutputWhy
"A man, a plan, a canal: Panama"true"amanaplanacanalpanama"
"race a car"false"raceacar"

Constraints: length 1 to 200,000, printable ASCII.

02How to spot it

  • "Reads the same backward": compare the string with its mirror.
  • "Ignore non-alphanumeric": junk can be skipped on the fly.
  • O(1) extra space wanted: no cleaned copy.

So: two pointers from both ends, moving inward.

03Pseudocode

text
l = 0 ; r = n - 1
while l < r:
  skip l forward past non-alphanumerics
  skip r backward past non-alphanumerics
  if lower(s[l]) != lower(s[r]): return false
  l += 1 ; r -= 1
return true
  1. Put one finger at each end.
  2. Skip anything that isn't a letter or digit.
  3. Compare letters, ignoring case, and move inward.

04Watch it work

Visualizer · step through the algorithms = "Race,car"
R0
a1
c2
e3
,4
c5
a6
r7
lr

One finger at each end. We only compare letters and digits, ignoring case.

Pseudocode

  1. l = 0 ; r = n - 1
  2. while l < r:
  3. skip l forward past non-alphanumerics
  4. skip r backward past non-alphanumerics
  5. if lower(s[l]) != lower(s[r]): return false
  6. l += 1 ; r -= 1
  7. return true

Variables

l
0
r
7
step 1 / 6

05Step-by-step dry run

  1. 1
    R0
    a1
    c2
    e3
    ,4
    c5
    a6
    r7
    lr

    One finger at each end. We only compare letters and digits, ignoring case.

    l = 0r = 7

  2. 2
    R0
    a1
    c2
    e3
    ,4
    c5
    a6
    r7
    lr

    'R' and 'r' lower-case to r: a match. Move both inward.

    l = 0r = 7a = rb = r

  3. 3
    R0
    a1
    c2
    e3
    ,4
    c5
    a6
    r7
    lr

    'a' and 'a' lower-case to a: a match. Move both inward.

    l = 1r = 6a = ab = a

  4. 4
    R0
    a1
    c2
    e3
    ,4
    c5
    a6
    r7
    lr

    'c' and 'c' lower-case to c: a match. Move both inward.

    l = 2r = 5a = cb = c

  5. 5
    R0
    a1
    c2
    e3
    ,4
    c5
    a6
    r7
    lr

    Skip ',': not a letter or digit.

    l = 3r = 3

  6. 6
    R0
    a1
    c2
    e3
    ,4
    c5
    a6
    r7

    The fingers met and every pair matched: it is a palindrome.

    l = 3r = 3

06Java solution

ValidPalindrome.java
public class ValidPalindrome {
    static boolean isPalindrome(String s) {
        int l = 0, r = s.length() - 1;
        while (l < r) {
            while (l < r && !Character.isLetterOrDigit(s.charAt(l))) l++;
            while (l < r && !Character.isLetterOrDigit(s.charAt(r))) r--;
            char a = Character.toLowerCase(s.charAt(l));
            char b = Character.toLowerCase(s.charAt(r));
            if (a != b) return false; // mismatch: not a palindrome
            l++;
            r--;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome("A man, a plan, a canal: Panama"));
        System.out.println(isPalindrome("race a car"));
        System.out.println(isPalindrome(".,"));
        System.out.println(isPalindrome("0P"));
    }
}
Expected output
true
false
true
false

Tricky lines, decoded

  • l < r inside the skip loops stops them running past each other on strings like ".,".
  • Character.isLetterOrDigit: digits count too, so "0P" is not a palindrome.

07Why it works

A palindrome's character at distance d from the start must equal the one at distance d from the end. The two pointers check exactly these pairs, skipping characters that don't count. If every pair matches before the pointers meet, the cleaned string equals its reverse.

Invariant: all letter and digit pairs outside [l, r] already match (ignoring case).

08Time and space

  • Time: O(n). Each pointer moves at most n steps in total.
  • Space: O(1). Building a cleaned, reversed copy would cost O(n) memory.

09Edge cases

InputOutputWhy it matters
" "truenothing left after cleaning
"a"truesingle character
".,"trueonly symbols
"0P"falsedigits count
"Aa"truecase-insensitive

10Common mistakes

  • Skipping without l < r. The pointers fly past each other and go out of bounds.
  • Character.isLetter. Digits must be compared, not ignored: use isLetterOrDigit.
  • Comparing before lower-casing. 'A' and 'a' are different chars.

11Other approaches

ApproachTimeSpaceUse when
Clean + reverseO(n)O(n)Quick to write
Two pointersO(n)O(1)Default answer
Regex cleanO(n)O(n)Scripting, not interviews
RecursionO(n)O(n)Stack overflow risk

12In the interview

  • Longest palindromic substring? (LeetCode 5) Expand around each centre, O(n²).
  • Unicode text? Use code points (codePointAt) so emoji and accents aren't split.

13Check your understanding

Quiz · 2 questions

0 of 2 answered

  1. 1. Why does the skip loop also check l < r?

  2. 2. Is "0P" a palindrome?

14Solve it yourself

Try it on LeetCode #125.