Valid Palindrome
Easy · Two Pointers · Two Pointers · String
Problem
A phrase is a palindrome if, after lowercasing and discarding everything that is not a letter or digit, it reads the same forwards and backwards.
Given a string s, return whether it is a palindrome.
Examples
Input: s = "A man, a plan, a canal: Panama" Output: True
"amanaplanacanalpanama" reversed is itself.
Input: s = "race a car" Output: False
"raceacar" is not a palindrome.
Input: s = " " Output: True
Nothing survives the filter, and an empty string is a palindrome.
Constraints
- 1 <= len(s) <= 2 * 10^5
- s is printable ASCII
Approaches
- Filter, then compare with the reverse — time O(n), space O(n)
Related problems
Start free on Cariara · Coding problems · System design studio