FastPrepValid Palindrome After Normalization

Valid Palindrome After Normalization

Kotak Mahindra Bank logoKotak Mahindra Bank● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a string s, determine whether its letters and digits form a palindrome when letter case is ignored.

Normalize the string conceptually using these rules:

  • Keep only ASCII letters A through Z, a through z, and digits 0 through 9.
  • Treat uppercase and lowercase versions of a letter as equal.
  • Keep the retained characters in their original order. Do not rearrange them or delete any retained letter or digit.

Return true if the retained sequence reads identically in both directions, and false otherwise. An empty retained sequence is a palindrome.

Function

isValidPalindrome(s: String) → boolean

Examples

Example 1

s = "A!b?b,a"return = true

Filtering punctuation and folding case gives abba, whose two outer pairs match.

Example 2

s = "0P"return = false

The digit 0 must be retained. The normalized sequence 0p has different outer characters.

Constraints

  • 0 <= s.length <= 20000.
  • Each character is printable ASCII, with a code from 32 through 126, inclusive.

More Kotak Mahindra Bank problems

See Kotak Mahindra Bank hiring insights
public boolean isValidPalindrome(String s) {
    // Write your code here
}
s"A!b?b,a"
expectedtrue
Checking account…