-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest Palindrome in a String.py
53 lines (41 loc) · 1.78 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
53
"""
https://practice.geeksforgeeks.org/problems/longest-palindrome-in-a-string/0
Given a string S, find the longest palindromic substring in S. Substring of string S: S[ i . . . . j ] where 0 ≤ i ≤ j < len(S). Palindrome string: A string which reads the same backwards. More formally, S is palindrome if reverse(S) = S. Incase of conflict, return the substring which occurs first ( with the least starting index ).
NOTE: Required Time Complexity O(n2).
"""
__author__ = 'abhireddy96'
class Solution:
def longestPalindrome(self, s: str) -> str:
# Is string is empty
if not s:
return s
# Contains single character
if len(s) == 1:
return s
# Let first character be resultant palindrome
res = s[0]
# Iterate over string characters
for i in range(0, len(s)):
# Check for Palindrome odd length
cur = self.palindrome_from_center(s, i, i)
if len(cur) > len(res):
res = cur
# Check for Palindrome even length
cur = self.palindrome_from_center(s, i, i + 1)
if len(cur) > len(res):
res = cur
return res
# find palindrome from center
def palindrome_from_center(self, s, begin, end):
# Traverse Outwards from given index
# Check that string index is not out of range
# left and right characters should match in order to be palindromic string
while begin >= 0 and end < len(s) and s[begin] == s[end]:
# Move towards left of string
begin -= 1
# Move towards right of string
end += 1
return s[begin + 1: end]
if __name__ == "__main__":
for _ in range(int(input())):
print(Solution().longestPalindrome(input()))