-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestpalindrome.py
52 lines (41 loc) · 1.54 KB
/
longestpalindrome.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 longestPalindrome(self, s: str) -> str:
palindromes = []
# return the index where a palindrome could be
indices= self.find(s)
indices_one = self.find_one(s)
# gothrough the list of palindromes
self.iterate_palin(s,indices,palindromes)
self.iterate_palin(s,indices_one,palindromes)
return max(palindromes)
def find(self, s):
indices = []
for i in range(len(s)-2):
if s[i] == s[i+2]:
indices.append(i)
return indices
def find_one(self, s):
indices_one = []
for i in range(len(s)-1):
if s[i] == s[i+1]:
indices_one.append((i,i+1))
return indices_one
def iterate_palin(self,s,indices,palindromes):
if indices:
for i in indices:
condition = True
j = 1
while condition is True:
if i-j >=0 and i+j < len(s):
if s[i-j] == s[i+j]:
j += 1
else:
condition = False
if
palindromes.append(s[i:i+2+1])
else:
palindromes.append(s[i:i+2+1])
condition = False
return
else:
return