-
Notifications
You must be signed in to change notification settings - Fork 1
/
substring.py
executable file
·55 lines (51 loc) · 1.43 KB
/
substring.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
import sys, time
def issub(a):
flag = 1
for i in range(len(a)-1):
if(a[i] != a[len(a)-1-i]):
flag = 0
return flag
def subbrute(a):
max = 0
werd = ""
for i in range(len(a)-1):
for j in range(len(a), i, -1):
#print(a[i:j])
#print(j-i)
if(issub(a[i:j])):
if j-i > max:
werd = a[i:j]
max = j-i
return max,werd
def subdyn(a):
#creating table to be used dynamically
tab = [[ False for x in range(len(a))] for y in range(len(a))]
#initialize diagonals to 1 (all substrings with length 1 are palindromes)
for i in range(len(a)):
tab[i][i] = True
"""do it for length 2
for i in range(len(a)-1):
if(a[i]==a[i+1]):
tab[i][i+1] = True"""
max = 1
#using previously calculated values, determine max palindrome substring
for inc in range(1,len(a)):
for i in range(len(a)-inc):
j = i+inc
if(a[i] == a[j] and tab[i+1][j-1]==True):
tab[i][j] = True
max = inc+1
return max
def main():
i =0
werd = ""
z,max = 0,0
for a in sys.argv:
if i==1:
#print(subbrute(a))
print(subdyn(a))
i+=1
#print(max);print(werd)
start_time = time.time()
main()
print(time.time() - start_time, "seconds")