-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
36 lines (30 loc) · 1.32 KB
/
Solution.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
class Solution(object):
def longestPalindrome(self, s):
if (s == s[::-1]):
return s
longest = ""
'''
# **Solution 1 (Takes too long)**
for i in range (0, len(s)):
for j in range (len(s)-1, i, -1):
if (s[i:j] == s[i:j][::-1]):
longest = longest if len(longest) >= len(s[i:j]) else s[i:j]
if (s[len(s)-1-j:len(s)-i] == s[len(s)-1-j:len(s)-i][::-1]):
longest = longest if len(longest) >= len(s[len(s)-1-j:len(s)-i]) else s[len(s)-1-j:len(s)-i]
'''
# **Solution 2**
for i in range(0, len(s)):
palindrome = ""
j = 0
# Looking for a palindrome of odd length
while (i-j >= 0) and (i+j < len(s)) and (s[i-j] == s[i+j]):
palindrome = s[i-j:i+j+1]
j += 1
longest = longest if len(longest) >= len(palindrome) else palindrome
j = 0
# Looking for a palindrome of even length
while (i-j >= 0) and (i+j+1 < len(s)) and (s[i-j] == s[i+j+1]):
palindrome = s[i-j:i+j+2]
j += 1
longest = longest if len(longest) >= len(palindrome) else palindrome
return longest