-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.py
More file actions
19 lines (17 loc) · 705 Bytes
/
LCS.py
File metadata and controls
19 lines (17 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#Longest Common Subsequence function
#Compares two strings returns Longest Common Subsequence as an integer
#03/07/2021 Modlizard
def lcs(a,b):
if type(a) != str or type(b) != str:
raise TypeError('Non-string comparison parameters provided!')
else:
calcArr = [[None]*(len(b)+1) for x in range(len(a)+1)]
for x in range(len(a)+1):
for y in range(len(b)+1):
if y == 0 or x == 0:
calcArr[x][y] = 0
elif a[x-1] == b[y-1]:
calcArr[x][y] = calcArr[x-1][y-1]+1
else:
calcArr[x][y] = max(calcArr[x-1][y], calcArr[x][y-1])
return calcArr[x][y]