Back

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, or false otherwise.

Test Cases

Copy an input into the main harness and Run to verify
Input
s = "A man, a plan, a canal: Panama"
Expected Output
true

Explanation: "amanaplanacanalpanama" is a palindrome.

Input
s = "race a car"
Expected Output
false
Input
s = " "
Expected Output
true

Explanation: After removing non-alphanumerics, s becomes an empty string, which reads the same both ways.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s consists only of printable ASCII characters.

Hints

Hint 1 — click to reveal

Two pointers from both ends, skipping non-alphanumerics.

Hint 2 — click to reveal

Compare lowercased characters as the pointers walk inward.

Java Compiler

Powered by OneCompiler. Starter code loads automatically — edit and hit Run.