Valid Palindrome

Easy Two pointers String

Problem

Return true if a string is a palindrome after converting letters to lowercase and removing every character that is not a letter or a digit.

Original: LeetCode 125: Valid Palindrome.

Examples

Example 1

Input`s = "A man, a plan, a canal: Panama"`
Output`true`

After removing non-alphanumeric characters and lowercasing, it reads "amanaplanacanalpanama".

Example 2

Input`s = "race a car"`
Output`false`

Example 3

Input`s = " "`
Output`true`

Nothing is left after filtering, and an empty string is a palindrome.

Constraints

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

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 125: Valid Palindromeleetcode.com
  2. Java SE 17 API: Character.isLetterOrDigitdocs.oracle.com