-
Notifications
You must be signed in to change notification settings - Fork 8
/
longestPalindromicSubstring.py
45 lines (40 loc) · 1.4 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
def isPalindromic(string):
left = 0
right = len(string)-1
while left <= right:
if string[left] != string[right]:
return False
left+=1
right-=1
return True
# O(N^3)time | O(1) space
def longestPalindromicSubstring(string):
currentLongestSubstring = ""
slow = 0
while slow < len(string):
fast = slow
while fast < len(string):
currentString = string[slow:fast+1]
print(currentString)
if isPalindromic(currentString) and len(currentLongestSubstring) <= len(currentString):
currentLongestSubstring = currentString
fast += 1
slow += 1
return currentLongestSubstring
# O(N^2) time | O(1) space
def longestPalindromicSubstring(string):
currentLongest=[0,1]
for i in range(1,len(string)):
odd=getLongestPalindromeFrom(string,i-1,i+1)
even=getLongestPalindromeFrom(string,i-1,i)
longest =max(odd,even,key=lambda x: x[1]-x[0])
currentLongest=max(longest,currentLongest,key=lambda x: x[1]-x[0])
return string[currentLongest[0]:currentLongest[1]]
def getLongestPalindromeFrom(string,leftIdx,rightIdx):
while leftIdx >= 0 and rightIdx < len(string):
if string[leftIdx] != string[rightIdx]:
break
leftIdx-=1
rightIdx+=1
return [leftIdx+1,rightIdx]
print(longestPalindromicSubstring("abaxyzzyxf"))