Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions week9/longest_common_subsequence.PY
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from itertools import combinations

def LCS(strArr):
first= strArr[0]
second= strArr[1]

firstsubs= []
secondsubs= []
for i in range(len(first)):
comb1= list(combinations(first, i))
comb2=[]
for j in comb1:
comb2.append(''.join(j))
firstsubs.extend(comb2)


for i in range(len(second)):
comb3= list(combinations(second, i))
comb4=[]
for j in comb3:
comb4.append(''.join(j))
secondsubs.extend(comb4)

firstsubs.remove('')
secondsubs.remove('')
if firstsubs == []:
firstsubs.append(first)
if secondsubs == []:
secondsubs.append(second)

sublength= 0

for i in firstsubs:
for j in secondsubs:
if i == j:
if len(i) > sublength:
sublength= len(i)


print(sublength)



# LCS(['bcacb', 'aacabb'])
LCS(['a', 'aa'])
# LCS(['c', 'b'])
14 changes: 14 additions & 0 deletions week9/longest_common_subsequence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
Author: Kayode
---

Longest Common Subsequence (LCS)
Have the function LCS(strArr) take the strArr parameter being passed which will be an array of two strings containing only the characters {a,b,c} and have your program return the length of the longest common subsequence common to both strings. A common subsequence for two strings does not require each character to occupy consecutive positions within the original strings. For example: if strArr is ["abcabb","bacb"] then your program should return 3 because one longest common subsequence for these two strings is "bab" and there are also other 3-length subsequences such as "acb" and "bcb" but 3 is the longest common subsequence for these two strings.


Examples
Input: ["abc","cb"]
Output: 1

Input: ["bcacb","aacabb"
Output: 3