Facebook PixelValid Palindrome — Coding Practice
Valid PalindromeEasy

Valid Palindrome

Easy 9.1k47% acceptance
Two PointersString

Given a string s, return true if it reads the same forwards and backwards once you consider only alphanumeric characters and ignore letter case; otherwise return false.

All non-alphanumeric characters (spaces, punctuation, symbols) are skipped. An empty string — or a string with no alphanumeric characters — counts as a palindrome.

Example 1
Input: s = "A man, a plan, a canal: Panama"
Output: true
Stripping non-alphanumerics and lowercasing gives "amanaplanacanalpanama", which is a palindrome.
Example 2
Input: s = "race a car"
Output: false
"raceacar" is not a palindrome.
Example 3
Input: s = " "
Output: true
After removing the space there are no characters left, which counts as a palindrome.
Constraints
  • 1 ≤ s.length ≤ 2·10⁵
  • `s` consists of printable ASCII characters.
Asked atMetaAmazonMicrosoftApple
JavaScript
Loading editor…
Case 1
"A man, a plan, a canal: Panama"
expected: true
Case 2
"race a car"
expected: false
Case 3
" "
expected: true
Case 4
""
expected: true
Case 5
"0P"
expected: false
Case 6
"ab_a"
expected: true
Case 7
"Was it a car or a cat I saw?"
expected: true