Problem Number: 125 Difficulty: Easy Category: String, Two Pointers LeetCode Link: https://leetcode.com/problems/valid-palindrome/
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.
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: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.
Constraints:
1 <= s.length <= 2 * 10^5sconsists only of printable ASCII characters
I used a String Manipulation approach with regular expressions. The key insight is to clean the string by removing non-alphanumeric characters and converting to lowercase, then check if it reads the same forward and backward.
Algorithm:
- Use regular expression to remove all non-alphanumeric characters
- Convert the cleaned string to lowercase
- Create a reversed version of the string
- Compare the original cleaned string with its reverse
- Return true if they are equal, false otherwise
The solution uses string manipulation with regular expressions. See the implementation in the solution file.
Key Points:
- Uses regex to remove non-alphanumeric characters
- Converts to lowercase for case-insensitive comparison
- Creates reversed string for comparison
- Simple and efficient approach
- Handles edge cases like empty strings
Time Complexity: O(n)
- Regex substitution: O(n)
- String reversal: O(n)
- String comparison: O(n)
- Total: O(n)
Space Complexity: O(n)
- Creates new strings for cleaned and reversed versions
- Each string can be up to length n
- Total: O(n)
-
String Cleaning: Using regex to remove non-alphanumeric characters is efficient and clean.
-
Case Insensitivity: Converting to lowercase ensures case-insensitive comparison.
-
Palindrome Check: Comparing a string with its reverse is a simple way to check for palindromes.
-
Character Filtering: Only alphanumeric characters matter for palindrome validation.
-
Edge Cases: Empty strings and strings with only non-alphanumeric characters are valid palindromes.
-
Built-in Functions: Using Python's built-in string operations makes the solution concise.
-
Manual Filtering: Initially might manually iterate through characters instead of using regex.
-
Case Sensitivity: Not converting to lowercase for case-insensitive comparison.
-
Complex Logic: Overcomplicating the palindrome check with two pointers.
-
Wrong Characters: Not properly filtering non-alphanumeric characters.
- Valid Palindrome II (Problem 680): Check if string can be palindrome after removing one character
- Longest Palindromic Substring (Problem 5): Find longest palindromic substring
- Palindrome Number (Problem 9): Check if number is palindrome
- Palindrome Linked List (Problem 234): Check if linked list is palindrome
- Two Pointers: Use left and right pointers to compare characters - O(n) time, O(1) space
- Stack-based: Use stack to reverse and compare - O(n) time, O(n) space
- Recursive: Use recursion to check palindrome - O(n) time, O(n) space
- Manual Filtering: Manually iterating through characters instead of using regex.
- Case Sensitivity: Not handling case-insensitive comparison.
- Complex Implementation: Using two pointers when simple string comparison suffices.
- Wrong Characters: Not properly filtering non-alphanumeric characters.
- Edge Cases: Not handling empty strings or strings with only special characters.
Note: This is a simple string manipulation problem that demonstrates efficient palindrome checking with regex.