CodeSpeek

Valid Palindrome

Easy · Two Pointers

You are given a string that may contain letters, digits, spaces and punctuation. Keep only the letters and digits, convert them all to lowercase, and check whether the resulting sequence reads the same forwards and backwards. Return true if it does, and false otherwise. An empty resulting sequence counts as a palindrome.

Examples

Input:  s = "A man, a plan, a canal: Panama"
Output: true
Why:    Ignoring punctuation, spaces and case, the letters read the same forwards and backwards.
Input:  s = "race a car"
Output: false
Why:    Stripped down to letters and digits it becomes 'raceacar', which is not a palindrome.
Input:  s = " "
Output: true
Why:    There are no letters or digits at all, so the empty result trivially reads the same both ways.

Constraints

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

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Valid Palindrome

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Valid Palindrome. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.