Suppose we have a string s; we have to check whether it is a palindrome or not. As we know a palindrome is when the word is the same forwards and backwards.
So, if the input is like s = "racecar", then the output will be True
To solve this, we will follow these steps −
- t := reverse of s
- if t is same as s, then
- return True
- otherwise,
- return False
Let us see the following implementation to get better understanding −
Example
class Solution:
def solve(self, s):
t=s[::-1] if t==s:
return True else :
return False
ob = Solution()
print(ob.solve("racecar"))Input
"racecar"
Output
True