How to Check if a String is a Palindrome
Q: How would you implement a function to check if a string is a palindrome using a data structure of your choice?
- Data Structures And Algorithms
- Mid level question
Explore all the latest Data Structures And Algorithms interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Data Structures And Algorithms interview for FREE!
To check if a string is a palindrome, I would use a stack data structure. A palindrome is a string that reads the same forwards and backwards, so by utilizing a stack, we can effectively reverse the string and compare it to the original.
Here’s how I would implement the function in Python:
```python
def is_palindrome(s: str) -> bool:
# Normalize the string by removing non-alphanumeric characters and converting to lower case
normalized_str = ''.join(char.lower() for char in s if char.isalnum())
# Use a stack to reverse the normalized string
stack = []
# Push all characters of the normalized string onto the stack
for char in normalized_str:
stack.append(char)
# Pop characters from the stack and compare with the normalized string
for char in normalized_str:
if char != stack.pop():
return False
return True
```
### Clarification:
1. Normalization: The function first normalizes the input string by removing any non-alphanumeric characters and converting it to lower case to ensure the comparison is case-insensitive and ignores spaces or punctuation.
2. Stack Usage: The stack stores the characters of the normalized string. When we pop characters off the stack, they come off in reverse order, which allows us to compare this reversed order with the original normalized string.
3. Time Complexity: This approach runs in O(n) time, where n is the length of the string, as we traverse the string a couple of times (once for normalization and once for comparisons).
4. Space Complexity: The space complexity is also O(n) due to the additional stack that stores the characters of the string.
### Examples:
- For the input "A man, a plan, a canal: Panama", the function would return `True`.
- For the input "race a car", the function would return `False`.
Here’s how I would implement the function in Python:
```python
def is_palindrome(s: str) -> bool:
# Normalize the string by removing non-alphanumeric characters and converting to lower case
normalized_str = ''.join(char.lower() for char in s if char.isalnum())
# Use a stack to reverse the normalized string
stack = []
# Push all characters of the normalized string onto the stack
for char in normalized_str:
stack.append(char)
# Pop characters from the stack and compare with the normalized string
for char in normalized_str:
if char != stack.pop():
return False
return True
```
### Clarification:
1. Normalization: The function first normalizes the input string by removing any non-alphanumeric characters and converting it to lower case to ensure the comparison is case-insensitive and ignores spaces or punctuation.
2. Stack Usage: The stack stores the characters of the normalized string. When we pop characters off the stack, they come off in reverse order, which allows us to compare this reversed order with the original normalized string.
3. Time Complexity: This approach runs in O(n) time, where n is the length of the string, as we traverse the string a couple of times (once for normalization and once for comparisons).
4. Space Complexity: The space complexity is also O(n) due to the additional stack that stores the characters of the string.
### Examples:
- For the input "A man, a plan, a canal: Panama", the function would return `True`.
- For the input "race a car", the function would return `False`.


