-
Notifications
You must be signed in to change notification settings - Fork 2
/
longestSubsequence2.py
executable file
·100 lines (63 loc) · 2.89 KB
/
longestSubsequence2.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
import datetime
def allSubseqRecursive(x):
l = []
l = []
for s in range(len(x)): # initial letter
for t in range(0,len(x)-s-1): # initial number of letters
l.append(x[s:s+t+1])
return list(set(l))
def allSubseq(x):
l = []
for s in range(len(x)): # initial letter
for t in range(0,len(x)-s-1): # initial number of letters
l.append(x[s:s+t+1])
# print x[s:s+t+1]
# print "x" * 20
for j in range(s+t+1,len(x)): # all the subsequent letters
for k in range(1,len(x)-j+1): # number of additional letters to first letter
l.append(x[s:s+t+1]+x[j:j+k])
# print s,t,j,k,
# print x[s:s+t+1]+x[j:j+k]
return list(set(l))
# allSubseqRecursive('abcd')
# exit()
def allContiguousSeq(x):
l = []
for s in range(len(x)): # initial letter
for t in range(0,len(x)-s): # initial number of letters
l.append(x[s:s+t+1])
return list(set(l))
def longestSubsequence(x,y):
if(len(x) == 0 or len(y) == 0):
return 0
xx = allSubseq(x)
# print xx
yy = allContiguousSeq(y)
# print yy
xx.sort(key=lambda item: (-len(item), item))
for i in xx:
if i in yy:
return len(i)
return 0
def main():
start_ts = datetime.datetime.now()
start_ts_fmt = str(datetime.datetime.now()).split('.')[0]
# x = 'hackerranks'
# y = 'hackers'
# x = 'abc'
# y = 'aedace'
x = 'abcdefghijklmnopqrstuvwxyz' * 7
# y = 'z'
# x = 'pxmfjrmvkehafjpxrehkkqcqbjpcmxymsgnfdzzplkdaewzoteyavwwzcnbtsrxyccjxfmbwsfquqelpicmmvymatfvwpemabhlxpjxuywludjhkfwpyowvnkpupalimnnecvtesblatxnewkywvvohdbgfavjxumqhvkznutjpowuvhmnyvzbykuzmchbnlmuiavdfbcuutaqqgiwhjefmcapfisdtohavoputtnhzecalriymlnfabvtbkhtnpenxkbtubuyskwykpablacspjkanwlnxeuuksccptvtqwomusmvuygfdmbkftbdlwmmxeudbdknqudfcrsaefetouygyejfelfqoqvhfabprdbjcihqrzfdbqcafvoowjskqwzironkxxsqedgbycvhnuskhdkkgfpggahvuznqytlldquvbofbxafrxmnbaignazengaxngdobatpmqfzghlamzuoelwvajlvzbuoxwluxqhsmcj'
# y = 'ohazmsexovixkuuneqnzdhhsweyjmrevqszglreqzacuzefaszzyzramuctxeusmzmtajezzfnrqmmmcyvrogrhntqwlbfpatgjxlweewaiaqidxrqplxdudscuqhrfjtqvksksnfmfhcodvghtkgzojpzytmdcimjzwaonnwmhmsaacnrblvvzhwbiokgziuvsfersuxiiydcfcvnkpbzljsqrqtgmhywkjxlxsixlxrwsnyypjkoxgtyczbouvojmfoqptnqfkvrynavuywnemedlvbvlafhorcfpqixphfwoybefcsbubegqmhcgyfbetfsyuqbadugfylowmzrifijkzlpawkewixgcfvqxapcyzpegrzrqczfdssgvspnjktlshhjqvvlkcmvwtwclpfwlwwulvfvmnnzldpiotcalpktbklalusufgbkrqgzdbagtqzlzealvq'
# print "x" * 80
print len(x)
allSubseq(x)
# print longestSubsequence(x,y)
# print "x" * 80
end_ts = datetime.datetime.now()
end_ts_fmt = str(datetime.datetime.now()).split('.')[0]
diff = end_ts - start_ts
print "{0} - {1} = {2}".format(start_ts_fmt, end_ts_fmt, diff)
main()