Palindrome check

Return True if s reads the same forwards and backwards, looking only at letters and digits and ignoring upper/lower case. "A man, a plan, a canal: Panama" is a palindrome.

Write is_palindrome(s) in Python, isPalindrome(s) in JavaScript or TypeScript, or int is_palindrome(char s[]) in C. Level: Easy.

Examples

Input: s = 'racecar'
Output: True
Input: s = 'A man, a plan, a canal: Panama'
Output: True
  • 0 ≤ length of s ≤ 1000
  • An empty string counts as a palindrome

Hint

First build a cleaned string: keep only letters and digits, in lower case. Then compare it with its reverse, or walk two pointers in from both ends.

The approach, step by step

  1. clean = letters and digits of s, lower case
  2. left = 0, right = last index
  3. left < right?
  4. clean[left] == clean[right]?
  5. left + 1, right − 1
  6. Return True
  7. Return False

← All practice problems

Try AI Flowchart free →