Valid Palindrome
Problem Statement
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 s, return true if it is a palindrome, or false otherwise.
Constraints:
- 1 <= s.length <= 2 * 10^5
- s consists only of printable ASCII characters
Input Format:
- A string s
Output Format:
- Return true if the string is a palindrome, false otherwise
Examples:
Example 1:
Input:
s = "A man, a plan, a canal: Panama"
Output:
true
Explanation:
"amanaplanacanalpanama" is a palindrome.
Example 2:
Input:
s = "race a car"
Output:
false
Explanation:
"raceacar" is not a palindrome.
Example 3:
Input:
s = " "
Output:
true
Explanation:
After removing non-alphanumeric characters, s is "". Since an empty string reads the same forward and backward, it is a palindrome.
Example 4:
Input:
s = "0P"
Output:
false
Explanation:
After removing non-alphanumeric characters, s is "0p". "0p" is not a palindrome.
Solutions
Two Pointers Approach
Use two pointers to compare characters from both ends of the string, ignoring non-alphanumeric characters.
In-place Two Pointers Approach
Use two pointers without creating a new string, skipping non-alphanumeric characters as we iterate through the original string.
Reverse and Compare Approach
Clean the string, create a reversed version, and compare the two strings.
Algorithm Walkthrough
Example input: nums = []
Step-by-step execution:
- Convert to lowercase: 'a man, a plan, a canal: panama'
- Remove non-alphanumeric characters: 'amanaplanacanalpanama'
- Initialize left = 0, right = 20 (length - 1)
- Compare s[0] = 'a' and s[20] = 'a': match, left = 1, right = 19
- Compare s[1] = 'm' and s[19] = 'm': match, left = 2, right = 18
- Compare s[2] = 'a' and s[18] = 'a': match, left = 3, right = 17
- Compare s[3] = 'n' and s[17] = 'n': match, left = 4, right = 16
- Compare s[4] = 'a' and s[16] = 'a': match, left = 5, right = 15
- Compare s[5] = 'p' and s[15] = 'p': match, left = 6, right = 14
- Compare s[6] = 'l' and s[14] = 'l': match, left = 7, right = 13
- Compare s[7] = 'a' and s[13] = 'a': match, left = 8, right = 12
- Compare s[8] = 'n' and s[12] = 'n': match, left = 9, right = 11
- Compare s[9] = 'a' and s[11] = 'a': match, left = 10, right = 10
- left is no longer less than right, so we exit the loop
- Return true, as all character comparisons matched
Hints
Hint 1
Hint 2
Hint 3
Video Tutorial
Video tutorials can be a great way to understand algorithms visually
Visualization
Visualize the two pointers moving from opposite ends of the string and comparing characters while skipping non-alphanumeric characters.
Key visualization elements:
- left pointer
- right pointer
- comparison
Implementation Notes
This is a commonly asked interview question that tests string manipulation and two pointers technique. It's important to handle edge cases such as empty strings, strings with only spaces, and strings with special characters.