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 * 105s consists only of printable ASCII characters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To check if a phrase is a palindrome using brute force, we essentially want to compare the phrase forwards and backwards. The simplest way is to create a reversed version of the phrase and then directly compare it to the original.
Here's how the algorithm would work step-by-step:
def is_palindrome_brute_force(input_phrase):
reversed_phrase = input_phrase[::-1]
# To determine if it's a palindrome, we must compare the original string with its reversed version.
if input_phrase == reversed_phrase:
# If every character matches in the same position, it is a palindrome.
return True
else:
# If any character does not match, it is not a palindrome.
return FalseTo check if a phrase is a palindrome, we can compare the characters from the beginning and end simultaneously. If they always match as we move inwards, the phrase is a palindrome.
Here's how the algorithm would work step-by-step:
def is_palindrome(input_string):
left_pointer = 0
right_pointer = len(input_string) - 1
while left_pointer < right_pointer:
# Advance left pointer past non-alphanumeric characters.
if not input_string[left_pointer].isalnum():
left_pointer += 1
continue
# Advance right pointer past non-alphanumeric characters.
if not input_string[right_pointer].isalnum():
right_pointer -= 1
continue
# Compare characters, ignoring case.
if input_string[left_pointer].lower() != input_string[right_pointer].lower():
return False
# Move pointers towards the center.
left_pointer += 1
right_pointer -= 1
# If pointers meet or cross without mismatches, it's a palindrome.
return True| Case | How to Handle |
|---|---|
| Empty string input | An empty string is considered a palindrome, so the function should return true. |
| String with only non-alphanumeric characters | After processing, the string becomes empty, which is a valid palindrome and should return true. |
| String with a single alphanumeric character | A single character string is always a palindrome, so the function should return true. |
| String with mixed case letters and numbers | All uppercase letters should be converted to lowercase, and only alphanumeric characters should be considered for the palindrome check. |
| String with all identical alphanumeric characters | Such strings are always palindromes, and the algorithm should correctly identify this. |
| Very long strings | The solution should be efficient in terms of time and space complexity to handle potentially large inputs without exceeding typical memory or time limits. |
| String with leading or trailing non-alphanumeric characters | These characters should be ignored during the processing and palindrome check. |
| String with unicode characters that are alphanumeric | Ensure the alphanumeric check correctly identifies and processes alphanumeric characters across different Unicode ranges if applicable, though standard ASCII is usually assumed. |