-
Notifications
You must be signed in to change notification settings - Fork 105
/
Longest Palindrome in a String.py
52 lines (41 loc) · 1.14 KB
/
Longest Palindrome in a String.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution:
def longestPalin(self, S):
# code here
fi = fj = 0
n = len(S)
for i in range(n):
# odd length palindrome with current index as center
j = i - 1
k = i + 1
while j >= 0 and k < n:
if S[j] != S[k]:
break
j -= 1
k += 1
if k-j-1 > fj-fi+1:
fi = j + 1
fj = k - 1
# even length palindrome if possible
if i < n-1 and S[i] == S[i+1]:
j = i - 1
k = i + 2
while j >= 0 and k < n:
if S[j] != S[k]:
break
j -= 1
k += 1
if k-j-1 > fj-fi+1:
fi = j + 1
fj = k - 1
return S[fi:fj+1]
#{
# Driver Code Starts
#Initial Template for Python 3
if __name__ == '__main__':
t = int(input())
for _ in range(t):
S = input()
ob = Solution()
ans = ob.longestPalin(S)
print(ans)
# } Driver Code Ends