- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlongestCommonSubstring.py
More file actions
Latest commit
31 lines (26 loc) · 735 Bytes
/
Copy pathlongestCommonSubstring.py
File metadata and controls
31 lines (26 loc) · 735 Bytes
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
defsorted_suffix_array(s):
out=list()
# O(n)
foriinrange(len(s)):
out.append(s[i:])
# O(log(n))
print(sorted(out))
returnsorted(out)
deflongest_common_substring(sa=[]):
result=""
# O(n)
foriinrange(len(sa)-1):
temp=""
forj, kinzip(sa[i], sa[i+1]):
ifj!=k:
break
temp+=j
# print("== ", temp, "++ ", result)
iflen(temp) >len(result):
result=temp
returnresult
if__name__=="__main__":
s1="abcba"
s2="cbadl"
# TODO: add sliding window to remove same string considered multiple times.
print(longest_common_substring(sorted_suffix_array(s1+"#"+s2+"%")))