Back

MakeMy Trip 2nd Question

Created 1 year ago
88 Views
2 Comments
DevolBq83
@DevolBq83
DevolBq83
@DevolBq83Profile is locked. Login

Given a string of length N, find the longest palindromic substring.
Input Size : 1 <= N <= 1000
Sample Testcase :
INPUT
DIEHEIDI
OUTPUT
DIEHEID


My Code:

s = input()
if s!=s[::-1]:
    for i in range(1, len(s)-1):
        p = [s[x:len(s)-i+x] for x in range(i) if s[x:len(s)-i+x] == s[x:len(s)-i+x][::-1]]
        if p:
            print(p[0])
            break
else:
    print(s)


It Fails the Test case but gives the desired output help me understand the question.

Comments (2)
Please login to comment.