-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-longest-palindrome.py
74 lines (62 loc) · 1.81 KB
/
06-longest-palindrome.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
# Find logest palindrome in a string
#
# s = 'aaaabbaa'
# answer: 'aabbaa'
#
def is_palendrome(s, l, r):
while(l<=r):
if(s[l] != s[r]):
return False
else:
l+=1
r-=1
return True
def max_palendrome(s):
if s == None or len(s) == 0:
return 0
max_found = 1
for size in range(1,len(s), 1):
for l in range(len(s)):
r = l+size
if(r>= len(s)):
break
if is_palendrome(s, l, r):
max_found = max(max_found, r-l+1)
return max_found
def max_palendrome_dynamic(s):
# set-up NxN matrix
m = [[0]*len(s) for i in range(len(s))]
# set matrix answers for all palindromes of size 1
# all single character strings are palindromes
for i in range(len(s)):
m[i][i] = 1
max_found = 1
# set matrix answers for all palindromes of size 2
size = 2
for i in range(0, len(s)-size+1, 1):
j = i+size-1
# i and j represent the start and end index of a substring
# of length 'size'
if s[i] == s[j]:
m[i][j] = 1
max_found = max(max_found, size)
# set matrix answers for all palindromes of size 3 to max
for size in range(3, len(s)+1, 1):
for i in range(0, len(s)-size+1, 1): # 0 1 2 3 4 (len:5)
j = i+size-1
# i and j represent the start index and end index of a substring
# of length 'size'
if s[i] == s[j] and m[i+1][j-1] == 1:
m[i][j] = 1
max_found = max(max_found, size)
return max_found
s = "aaaabbaa"
print(max_palendrome(s))
print(max_palendrome_dynamic(s))
s = "cbabacb"
print(max_palendrome(s))
print(max_palendrome_dynamic(s))
s = "qracbzbcarq"
print(max_palendrome(s))
print(max_palendrome_dynamic(s))