Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Palindrome With a Twist
00:00
5 left

Palindrome With a Twist

MediumPython

Problem

UKG Ready may need to validate short employee-facing message tokens that contain inconsistent capitalization, spaces, and punctuation. Given a string, determine whether it can become a palindrome after ignoring non-alphanumeric characters, ignoring letter case, and deleting at most one alphanumeric character.

A deletion is optional, and only one normalized character may be removed. Return True if the normalized string already is a palindrome or can become one with one deletion. Otherwise, return False.

Formal Specification

Implement valid_near_palindrome(s), where s is a string. Return a boolean.

Normalization keeps only alphanumeric characters and converts letters to lowercase. The result is valid if it reads identically from left to right and right to left, either before deletion or after removing at most one normalized character.

Constraints

  • 1 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Normalization keeps letters and digits and ignores all other characters
  • At most one normalized character may be deleted

Function Signature

def valid_near_palindrome(s):
Interviewer

Your question is Palindrome With a Twist. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.