Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Implement Palindrome
00:00
5 left

Implement Palindrome

EasyPython

Problem

Elsevier content-processing services may need to validate whether a user-provided phrase reads the same forward and backward. Implement isPalindrome to determine whether a string is a palindrome after ignoring case and all characters that are not letters or digits.

Formal Specification

Given a string s, return True if the sequence of alphanumeric characters in s, compared without regard to case, is identical from left to right and right to left. Otherwise, return False.

The function must not modify the input string. An empty string or a string containing no alphanumeric characters is considered a palindrome.

Examples

Example 1

Input: s = "A man, a plan, a canal: Panama"
Output: True

Ignoring punctuation and spaces, the string becomes amanaplanacanalpanama, which reads identically in both directions.

Example 2

Input: s = "Elsevier"
Output: False

The normalized string starts with e and ends with r, so it cannot be a palindrome.

Constraints

  • 0 <= len(s) <= 2 * 10^5
  • s contains printable ASCII characters.
  • Comparisons are case-insensitive.
  • Only letters and digits participate in the comparison.

Constraints

  • 0 <= len(s) <= 2 * 10^5
  • s contains printable ASCII characters
  • Comparisons are case-insensitive
  • Only letters and digits participate in the comparison

Function Signature

def isPalindrome(s):
Interviewer

Your question is Implement Palindrome. 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.