In this article, I will be explaining how to solve the Valid Palindrome problem on LeetCode. This is an Easy-level problem.
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Given a string
true if it is a palindrome, or
Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome.
Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome.
Input: s = " " Output: true Explanation: s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
Alphanumeric characters are the lowercase letters a-z, capital letters A-Z, and integers 0-9.
So, to solve this, we first want to remove all non-alphanumeric characters, including spaces, punctuation, etc., from the input string. Then, we need to check the new string to see if it’s a palindrome. We have two ways we can do this.
For this solution, we create a new string which contains all of the alphanumeric characters from the input string (lowercased). Then, we compare this new string to its reversed version. If they are the same, the function returns true. If not, it returns false.
In order to create the new string, we loop through the characters in the original input string and, if the character is alphanumeric, we append it to the new string.
Finally, we can check if the string (without any special characters or spaces) is a palindrome by using a boolean expression to equate it to its reverse. We can get the reverse of a string by using str[::-1]
def isPalindrome(self, s: str) -> bool: newStr = "" for char in s: if char.isalnum(): newStr += c.lower() return newStr == newStr[::-1]
This solution has a time complexity of O(n) because we have to loop through the string to remove non-alphanumeric characters. It also has a space complexity of O(n) because we are creating another string, which could be at most the size of the input string.
For this solution, we will create pointers to the beginning and end of the array which will make sure that the two sides have the same characters in the same order.
First, create two pointers— one which points to the beginning of the string and the other which points to the end. The two pointers will move inwards, checking each character until the reach the middle or overlap with each other.
As the pointers loop through the characters, they will skip any characters that are not alphanumeric. To do this, we use while loops that will continue to run until the character at the
right index is alphanumeric. We also add a check to make sure that the left/right index is in bounds by checking
left < right /
right > left
We must make sure to compare the lowercase versions of the characters to each other. If any character at the left pointer does not match the corresponding character at the right pointer, then we know that it’s not a palindrome and can return False. If the entire loop finishes then that must mean the string is a palindrome and we can return True.
def isPalindrome(self, s: str) -> bool: left, right = 0, len(s) - 1 while left < right: while left < right and not s[left].isalnum(): left +=1 while right > left and not s[right].isalnum(): right -= 1 if s[left].lower() != s[right].lower(): return False left, right = left + 1, right - 1 return True
The time complexity for this solution is O(n) because you are still looping through the entire string. The space complexity is O(1) because you are not making a new string.