-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLongestPalindromicSubstring.py
48 lines (40 loc) · 1.27 KB
/
LongestPalindromicSubstring.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
"""
@author Anirudh Sharma
Given a string s, return the longest palindromic substring in s.
Constraints
1 <= s.length <= 1000
s consist of only digits and English letters (lower-case and/or upper-case).
"""
def expandFromCenter(s, left, right):
# Base case
if s is None or left > right:
return 0
# Loop until the characters on left and right are same
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return right - left - 1
def longestPalindrome(s):
# Special case
if s is None or len(s) == 0:
return ""
# Start and end pointers for the substring
start, end = 0, 0
# Loop through the entire string
for i in range(len(s)):
# String with even length
a = expandFromCenter(s, i, i)
# String with odd length
b = expandFromCenter(s, i, i + 1)
# Maxmum of the two lengths
c = max(a, b)
# Get the length of the longest palindromic substring
if c > end - start:
start = i - (c - 1) // 2
end = i + c // 2
return s[start:(end + 1)]
if __name__ == "__main__":
print(longestPalindrome("babad"))
print(longestPalindrome("cbbd"))
print(longestPalindrome("a"))
print(longestPalindrome("ac"))