xxxxxxxxxx
value = input("Enter a Word: ")
if value == value[::-1] :
print(value)
print(value[::-1])
print("THIS WORD IS A PALINDROME")
else :
print(value)
print(value[::-1])
print("THIS WORD IS NOT A PALINDROME")
xxxxxxxxxx
s = input('enter string: ')
def palindrome(string):
x = ""
for i in string:
x = i + x
return x
if s == palindrome(s):
print('its a palindrome')
else:
print('its not a palindrome')
xxxxxxxxxx
num=int(input("Enter a no.:"))
rev=0
num1=num
while num!=0:
rev=rev*10+(num%10)
num=num//10
if num1==rev:
print(num1," is a palindrome")
else:
print(num1," is not a palindrome")
xxxxxxxxxx
myString = "aba"
if myString == myString[::-1]:
print("The string '" + myString + "' is a palindrome")
else:
print("The string '" + myString + "' is not a palindrome")
xxxxxxxxxx
def is_palindrome(s):
reverse = s[::-1]
if (s == reverse):
return True
return False
s1 = 'racecar'
s2 = 'hippopotamus'
print('\'racecar\' a palindrome -> {}'.format(is_palindrome(s1)))
print('\'hippopotamus\' a palindrome -> {}'.format(is_palindrome(s2)))
# output
# 'racecar' is a palindrome -> True
# 'hippopotamus' is a palindrome -> False
xxxxxxxxxx
n = input("Enter the word and see if it is palindrome: ") #check palindrome
if n == n[::-1]:
print("This word is palindrome")
else:
print("This word is not palindrome")
print("franco")
xxxxxxxxxx
string = input("Type a string: ")
if string[::-1] == string:
print(string,"This string is Palindrome")
else:
print(string,"This string is not Palindrome")
xxxxxxxxxx
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
xxxxxxxxxx
"""
This implementation checks whether a given
string is a palindrome. A string is
considered to be a palindrome if it reads the
same forward and backward. For example, "kayak"
is a palindrome, while, "door" is not.
Let n be the length of the string
Time complexity: O(n),
Space complexity: O(1)
"""
def isPalindrome(string):
# Maintain left and right pointers
leftIdx = 0
rightIdx = len(string)-1
while leftIdx < rightIdx:
# If chars on either end don't match
# string cannot be a palindrome
if string[leftIdx] != string[rightIdx]:
return False
# Otherwise, proceed to next chars
leftIdx += 1
rightIdx -= 1
return True
print(isPalindrome("kayak")) # True
print(isPalindrome("door")) # False