-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). | ||
For example, "radar" and "A man, a plan, a canal, Panama!" are palindromes. | ||
Write a function called is_palindrome that takes a string as input and returns True if the string is a palindrome, and False otherwise. | ||
The function should be case-insensitive and should ignore spaces and non-alphanumeric characters. | ||
Constraints: | ||
The input string will contain only printable ASCII characters. | ||
The maximum length of the input string will not exceed 10^4. | ||
During the interview, I would ask you the following questions: | ||
""" | ||
|
||
import re | ||
|
||
def is_palindrome(s: str) -> bool: | ||
clean_s = re.sub(r"[!@#$%^?/,()!&*]", "", s.lower().replace(" ", "")) | ||
stringList = list(clean_s) | ||
|
||
return ''.join(stringList[::-1]) == clean_s | ||
|
||
print(is_palindrome("radar")) # Output: True | ||
print(is_palindrome("A man, a plan, a canal, Panama!")) # Output: True | ||
print(is_palindrome("hello")) # Output: False | ||
print(is_palindrome("race a car")) # Output: False |