ExamAdda Logo

Valid Palindrome

Easy

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.

Given a string s, return true if it is a palindrome. Otherwise, return false.

Example 1

Input

A man, a plan, a canal: Panama

Output

true

Explanation

After removing non-alphanumeric characters and converting to lowercase, the string becomes "amanaplanacanalpanama", which is a palindrome.

Example 2

Input

race a car

Output

false

Explanation

After preprocessing, the string becomes "raceacar", which is not the same when read backward.

Constraints

1 ≤ s.length ≤ 2 × 10⁵
s consists of printable ASCII characters.

Hints:

Hint 1

Use two pointers, one starting from the beginning and the other from the end of the string.

Hint 2

Skip non-alphanumeric characters and compare the remaining characters after converting them to lowercase.

Author & Technical Reviewer

Chatla Jithin Sri Surya

Technically reviewed by: Chatla Jithin Sri Surya

Content Writer

<p>IIEST Shibpur | SDE | AI, ML &amp; Gen AI | DSA | Building Intelligent, Scalable Software</p>

Published

Oct 12, 2026

Last updated

Oct 12, 2026

Last technically reviewed

Oct 12, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output