+ 2
Why didn't it work properly??
You are given a string (STR) of length N. Your task is to find the longest palindromic substring My code is: a = input() n = len(a) for i in range(n,-1,-1): if a[i::-1] == a[:i]: print(a[i::-1])
4 Answers
+ 5
finding the longest palindrome in a string
here is try that i did, that uses the technique previously described in my first answer. please feel free to test it.
it is searching for the longest palindrome. if there are multiple palindromes with the same length, it uses the first that appears. if it necessary to list multiple palindromes of the same length, the code can be modified.
#inp = "uplevels"
inp = "abbaottosvenracecarxray"
print(inp)
max_ = 0
sub_ = ""
for start in range(len(inp)):
for width in range(start + 1,len(inp) + 1):
rng = inp[start:width]
if rng == rng[::-1] and len(rng) > max_:
max_ = len(rng)
sub_ = rng
print(max_, sub_)
+ 4
Shahir ,
we need to know how your string is composed of.
âȘïžstring sample: "abba otto sven racecar xray" if it contains words separated by space (or an other separator), you can split the string by the separator. this creates a list of words.
then you can iterate through this list and check if a word is a palindrome or not. the longest palindrome is "racecar"
âȘïžstring sample: "abbaottosvenracecarxray" if it contains no separators you have to iterate through the string by using a technique that is called "sliding window". you may start with a window that has a size of e.g 2 , that moves over the string in a step width of 1. each window has to be checked if it is a palindrome or not.
when reaching the end of the string, it starts again from the beginning of the string but with a window size that is increased by one. the longest palindrome is "racecar".
this task needs more coding the first task
+ 2
"""You are given a string (STR) of length N.
Your task is to find the longest palindromic substring
My code is:"""
a = input()
n = len(a)
for i in range(n,0,-1):
if a[i-1::-1] == a[:i]:
print(a[i-1::-1],a[:i])
Now??
+ 2
Input is given in the form of a word not by the sentence