# [Interview Question] How to Check a Valid Palindrome?

Rate this post

[toc]

Company tags: Apple, Amazon, Bloomberg, Facebook, Microsoft, Oracle

This is one of the most commonly occurred questions in numerous interviews and you should be absolutely ready to solve this question in a flash as soon as you see this in your interview. Hence, let’s dive into the problem and solution to this interview question.

## Problem Statement

Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

⚠️Constraints:

1. `1 <= s.length <= 2 * 105`
2. `s` consists only of printable ASCII characters.

## Examples

Let us look at some examples to improve the understanding of the problem:

Now that you have a clear understanding of the problem, let’s dive into various methods to solve the problem:

## Method 1: Using Slicing

Approach: The most basic approach to check whether the given string is a valid palindrome or not is to compare the original string with the reversed string. You can reverse the given string with help of slicing. Remember to eliminate the characters that are not alphanumeric before using the slice operation on the string. Return true if the original string and the reversed string are equal. Otherwise, return false.

? A Quick Recap to Slicing in Python
Slicing is a Python concept to carve out a substring from a given string `s` by using the square bracket notation `s[start:stop:step]` specifying the `start` index, the `stop` index, and the `step` size. You can set default start and stop indices and use negative step size -1 to reverse a given string in Python. The respective code would be `s[::-1]` to reverse string `s`.

Algorithm:

1. Initialize an empty string that will store only the alphanumeric characters of the original string.
2. For every character in the string, check whether it is alphanumeric. If yes, add the lowercase character to the new string.
3. Compare the reversed string with the original string. If equal return True, else returns False.

Solution:

```def valid_palindrome(s):
new_s = ''
for c in s:
if c.isalnum():
new_s = new_s + c.lower()

if new_s[::-1] == new_s:
return True
else:
return False```

Test Case Analysis: Let’s run the above code on our examples:

Yeah! It passed all the test cases.

Complexity Analysis:

• Time Complexity: The time complexity of this method is O(n), as we have traversed the string linearly only once.
• Space Complexity: The space complexity of this method is O(n) as extra space is used to store the reversed string.

## Method 2: Using Two Pointers

Approach: In this approach, we will use two pointers- one at the start and another at the end of the string to check whether the string is a valid palindrome. The two-pointer approach helps to save the extra memory used in the previous method.

Algorithm:

1. Initialize two pointers start and end at the start and end of the string.
2. Keep checking if the character at both the pointers is alphanumeric till the value of the start pointer is less than the value of the end.
3. If the characters at both the start and end pointers are alphanumeric, check if both the characters are the same. (Use lower() to ignore the case)
4. If the characters are not equal, return False.
5. If the loop ends, it implies the string is a valid palindrome, hence return True.

The following illustration demonstrates the working principle of the above algorithm. The gven string in this case is “1,M om 1“.

Solution:

```def valid_palindrome(s):
start = 0
end = len(s) - 1
while start < end:
while start < end and not s[start].isalnum():
start = start + 1
while start < end and not s[end].isalnum():
end = end - 1
if s[start].lower() != s[end].lower():
return False

start = start + 1
end = end - 1

return True```

Test Case Analysis: Let’s run this on our examples.

Yeah! It passed all the test cases.

Complexity Analysis:

• Time Complexity: The time complexity of this method is O(n) as we visit every character of the string only once.
• Space Complexity: The space complexity of this method is O(1), as no extra space gets used.

## Method 3: Using Regular Expressions

Approach: This method is the most Pythonic way you can use to solve the problem. Import the Python built-in package re to check whether the string is a valid palindrome.

Reg-ex module in Python: To work with regular expressions, Python has a built-in module called regex module. There are various functions in this module that can be used on the strings.

To check if the string is a valid palindrome, you have to use re.sub() on a set of alphanumeric characters that need to be replaced with its lowercase character.

Do you want to master the regex superpower? Check out the new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning:
(1) study a book chapter,
(2) solve a code puzzle, and
(3) watch an educational chapter video.

Solution:

```import re
def valid_palindrome(s):
s = re.sub('[^0-9a-zA-Z]', '', s.lower())

if s[::-1] == s:
return True
else:
return False```

Test Case Analysis: Let’s run this on our examples.

Yeah! It passed all the test cases.

Complexity Analysis: The time complexity of this method is O(n) as we visit every character of the string only once.

## Conclusion

I hope you enjoyed this coding interview question. Please stay tuned and subscribe for more interesting coding problems.

?‍? Post Credits: Shubham Sayon and Rashi Agarwal

Recommended: Finxter Computer Science Academy

• One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
• So, do you want to master the art of web scraping using Python’s BeautifulSoup?
• If the answer is yes – this course will take you from beginner to expert in Web Scraping.