Valid Palindrome
Explore how to efficiently validate if a string is a palindrome using the two pointers technique. Learn to ignore non-alphanumeric characters and case differences while solving this common coding interview problem. This lesson helps you break down the problem and implement a clear solution in a hands-on coding environment.
We'll cover the following...
Statement
Given a string, s, return TRUE if it is a palindrome; otherwise, return FALSE.
A phrase is considered a palindrome if it reads the same backward as forward after converting all uppercase letters to lowercase and removing any characters that are not letters or numbers. Only alphanumeric characters (letters and digits) are taken into account.
Constraints:
s.lengthsconsists only of printable ASCII characters.
Examples
Understand the problem
Let’s take a moment to make sure you've correctly understood the problem. The quiz below helps you check if you're solving the correct problem:
Valid Palindrome
What is the output for the following input?
s = “A man, a plan, a canal: Panama”
TRUE
FALSE
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding on how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
Need a nudge?
Explore these hints—each one is designed to guide you a step closer to the solution.
def is_palindrome(s):left, right = 0, len(s) - 1while left < right:while left < right and not s[left].isalnum():left += 1while left < right and not s[right].isalnum():right -= 1if s[left].lower() != s[right].lower():# Replace this placeholder "pass" statement with your codepassleft += 1right -= 1return True