I have made this so far
function lcs(xstr, ystr)
if xstr:len() == 0 or ystr:len() == 0 then
return ""
end
x = xstr:sub(1,1)
y = ystr:sub(1,1)
xs = xstr:sub(2)
ys = ystr:sub(2)
if x == y then
return x .. lcs(xs, ys)
else
l1 = lcs(xstr, ys)
l2 = lcs(xs, ystr)
if l1:len() > l2:len() then
return l1
else
return l2
end
end
end
print(lcs("abcd", "bcd"))
unfortunately it prints just "d" and not "bcd" as expected. For me it looks like the line "l2 = lcs(xs, ystr)" hasn't be executed because if I add debug print at the beginning it prints that function hasn't been called wit arguments "bcd" and "bcd", but I'm sure that values are alright after beginning of else statement. I would appreciate any help.
Your xs
variable is global
l1 = lcs(xstr, ys)
l2 = lcs(xs, ystr)
First line corrupts xs
value used by second line.
Make all your temporary variables (x, y, xs, ys, l1, l2) local.