I want to create a brute force algorithm to find the largest common subsequence between 2 strings, but I'm struggling to enumerate all possibilities in the form of a algorithm.
I don't want a dynamic programming answer as oddly enough I manage to figure this one out (You would think the brute force method would be easier). Please use pseudo code, as I prefer to understand it and write it up myself.
It's pretty much the same as DP minus the memoization part.
LCS(s1, s2, i, j):
if(i == -1 || j == -1)
return 0
if(s1[i] == s2[j])
return 1 + LCS(s1, s2, i-1, j-1)
return max(LCS(s1, s2, i-1, j), LCS(s1, s2, i, j-1))
The idea is if we have two strings s1 and s2 where s1 ends at i and s2 ends at j, then the LCS is:
The time complexity is obviously exponential.
I like @turingcomplete's answer but it's not really brute-force since it doesn't actually enumerate all candidate solutions. For example, if the strings are "ABCDE" and "XBCDY", the recursive approach won't test for "ABC" versus "XBC" because the test for "A" versus "X" will have already failed. It's kind of a matter of opinion whether you want to count that as a unique candidate though. In fact, you could argue that "ABC" versus "ABCDY" is a valid candidate as well, and just immediately fails due to length difference. You could add separate LA and LB to the code below to fully enumerate those candidates though.
for L = min(A.length, B.length) to 1
{
for iA = 0 to A.length - L - 1
{
for iB = 0 to B.length - L - 1
{
for i = 0 to L - 1
{
if(A[iA] != B[iB])
{
match failed;
}
}
if match didn't fail, then
A[iA..iA+L] and B[iB..iB+L] are a maximal common substring
}
}
}
no common substring
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With