AAlgoLoopSpaced repetition for LeetCode
EASYTwo PointersLeetCode ↗

Valid Palindrome

The key idea

Walk one pointer in from each end. Skip any character that is not a letter or digit, compare the rest case-insensitively, and they must match in mirror order. This checks the palindrome in place without building a cleaned copy of the string.

Problem

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

Examples

Input: s = "A man, a plan, a canal: Panama" Output: true
Input: s = "race a car" Output: false
Input: s = " " Output: true

Complexity

Time: O(n) Space: O(1)

See the full solution

410310
Step-by-step visualization
Start free →

More Two Pointers problems