Back to the 100
Problem 005Two Pointers
Easy

Valid Palindrome

005

Return true if s is a palindrome after converting uppercase letters to lowercase and removing all non-alphanumeric characters. Otherwise, return false.

EXAMPLES

Example 1
Input
{
  "s": "A man, a plan, a canal: Panama"
}

Output
true

FUNCTION SHAPE

s: stringbool
SOLUTION NOTE

We basically use two pointers. We initialize from both ends of the string. We loop while the string is more than 1 character long. We iterate until the left and right pointers are alphanumeric. We then check if the lowercase equivalents are equal, if not this string is not a palindrome. Otherwise these 2 characters match and we continue.

Open on LeetCode
00:00
3 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.