+ 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])

19th Aug 2021, 2:34 PM
Shahir
Shahir - avatar
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_)
19th Aug 2021, 4:42 PM
Lothar
Lothar - avatar
+ 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
19th Aug 2021, 3:53 PM
Lothar
Lothar - avatar
+ 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??
19th Aug 2021, 3:14 PM
Shahir
Shahir - avatar
+ 2
Input is given in the form of a word not by the sentence
19th Aug 2021, 3:56 PM
Shahir
Shahir - avatar