AlgoInsight

DSA problem

Valid Palindrome

EasyStringsPattern: Two Pointerstop150FacebookAmazonMicrosoftApple

A phrase is a palindrome if, after converting all uppercase letters into lowercase and removing all non-alphanumeric characters, it reads the same forward and backward.

Alphanumeric characters include letters and digits. Given a string s, return true if it is a palindrome, or false otherwise.

For example, "A man, a plan, a canal: Panama" reduces to "amanaplanacanalpanama" and is a valid palindrome, while "race a car" reduces to "raceacar" which is not.

Examples

Example 1
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: After lowercasing and removing non-alphanumeric characters, s becomes "amanaplanacanalpanama", which reads the same both ways.
Example 2
Input: s = "race a car"
Output: false
Explanation: After cleaning, s becomes "raceacar", which is not a palindrome.
Example 3
Input: s = " "
Output: true
Explanation: After removing non-alphanumeric characters, s is the empty string, and an empty string is considered a palindrome.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringtwo-pointers

Browse all DSA problems