Valid Palindrome
Clarify the problem:
- The problem requires determining whether a given string is a valid palindrome.
- A palindrome is a string that reads the same forwards and backwards, ignoring non-alphanumeric characters and considering case insensitivity.
- We need to return a boolean value indicating whether the string is a valid palindrome or not.
Analyze the problem:
- Input: A string.
- Output: A boolean value indicating whether the string is a valid palindrome.
- Constraints:
- The input string can contain alphanumeric characters and non-alphanumeric characters.
- The input string can have leading or trailing spaces.
- The comparison should be case-insensitive.
Design an algorithm:
- To solve the problem, we can use a two-pointer approach.
- We can initialize two pointers, one at the beginning of the string and the other at the end.
- While the two pointers haven't crossed each other, we compare the characters at the pointers.
- If the characters are alphanumeric and match, we move the pointers towards each other.
- If the characters are not alphanumeric or they don't match, we return False, indicating that the string is not a valid palindrome.
- If the pointers cross each other without encountering any non-matching characters, we return True, indicating that the string is a valid palindrome.
Explain your approach:
- To solve the problem, we will use a two-pointer approach to check for palindrome validity.
- We will define two pointers,
left
andright
, initialized to the start and end of the string, respectively. - We will use a while loop to compare characters at the pointers while they haven't crossed each other.
- Inside the loop, we will check if the characters at the pointers are alphanumeric and if they match (ignoring case).
- If the characters are not alphanumeric or they don't match, we return False.
- If the characters are alphanumeric and they match, we increment
left
and decrementright
to continue comparing the next characters. - Finally, if the pointers cross each other without any non-matching characters, we return True.
Write clean and readable code:
python
def isPalindrome(s):
left = 0
right = len(s) - 1
while left < right:
while left < right and not s[left].isalnum():
left += 1
while left < right and not s[right].isalnum():
right -= 1
if left < right and s[left].lower() != s[right].lower():
return False
left += 1
right -= 1
return True
Test your code:
Test Case 1:
- Input: s = "A man, a plan, a canal: Panama"
- Explanation: The input string is "A man, a plan, a canal: Panama". Ignoring non-alphanumeric characters and case, it reads the same forwards and backwards.
- Expected output: True
Test Case 2:
- Input: s = "race a car"
- Explanation: The input string is "race a car". Ignoring non-alphanumeric characters and case, it does not read the same forwards and backwards.
- Expected output: False
Test Case 3:
- Input: s = "abccba"
- Explanation: The input string is "abccba". Ignoring non-alphanumeric characters and case, it reads the same forwards and backwards.
- Expected output: True
Optimize if necessary:
- The algorithm has a time complexity of O(n), where n is the length of the input string. We iterate through the string once using the two-pointer approach.
- The space complexity is O(1) since we only use a constant amount of space for the two pointers.
Handle error cases:
- The code can handle empty strings or strings with only non-alphanumeric characters correctly.
- The algorithm will return True for empty strings or strings with no alphanumeric characters since they can be considered valid palindromes.
Discuss complexity analysis:
- The time complexity of the algorithm is O(n), where n is the length of the input string. We iterate through the string once using the two-pointer approach.
- The space complexity is O(1) since we only use a constant amount of space for the two pointers.