Menu
CoddyTech

Valid Palindrome

You get a string s. Keep only its letters and digits, treat upper and lower case as the same letter, and decide whether what is left reads the same from left to right as from right to left. Return true if it does and false otherwise.

Every other character, such as ., !, ?, :, ;, - or _, is ignored. If s has no letters or digits at all, nothing is left, and an empty text counts as a palindrome.

Function

isPalindrome(s: string) → boolean
sstring
the text to check, punctuation included
Returnsboolean
true if the letters and digits of s read the same in both directions, ignoring case

Constraints

  • 1 ≤ s.length ≤ 5 × 104
  • s contains English letters, digits and the punctuation marks . ! ? : ; - _, with no spaces.

Examples

Input
s = "Was_it_a_car_or_a_cat_I_saw?"
Output
true
Explanation
Drop the underscores and the question mark and lower the capitals: you get wasitacaroracatisaw, which is the same backwards.

lock icon+25 hidden tests on Submit

challenge icon

Follow-up

Can you decide it with O(1) extra memory, without building a cleaned copy of s?

Reset code
def isPalindrome(s):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

s = "Was_it_a_car_or_a_cat_I_saw?"

Expected

true