Validate Palindrome (Easy)
Good evening! Here's our prompt for today.
You may see this problem at Microsoft, Amazon, Linkedin, Stripe, Paypal, Yelp, Airbnb, Reddit, Datadog, Rubrik, Gusto, Lucid Software, Digitalocean, Servicenow, Zoom, Intel, and Zoominfo.
Given a string str
, can you write a method that will return True
if is a palindrome and False
if it is not? If you'll recall, a palindrome
is defined as "a word, phrase, or sequence that reads the same backward as forward". For now, assume that we will not have input strings that contain special characters or spaces, so the following examples hold:
1str = 'thisisnotapalindrome'
2is_palindrome = is_palindrome(str)
3# False
4
5str = 'racecar'
6is_palindrome = is_palindrome(str)
7# True
For an extra challenge, try to ignore non-alphanumerical characters. The final solution that we present will handle all edge cases.
Constraints
- Length of the given string <=
100000
- The string will consist of ASCII characters (some or all)
- Expected time complexity :
O(n)
- Expected space complexity :
O(1)
xxxxxxxxxx
35
def is_palindrome(s):
# fill in
return True
# print(is_palindrome('A Santa Lived As a Devil At NASA'));
import unittest
class Test(unittest.TestCase):
def test_1(self):
assert is_palindrome("A Santa Lived As a Devil At NASA") == True
print(
"PASSED: assert is_palindrome('A Santa Lived As a Devil At NASA') == True"
)
def test_2(self):
assert is_palindrome("gold") == False
print("PASSED: assert is_palindrome('gold') == False")
def test_3(self):
assert is_palindrome("a") == True
print("PASSED: assert is_palindrome('a') == True")
def test_4(self):
OUTPUT
Results will appear here.