Hi, I have written the following function to calculate the Longest Common Subsequence between two strings:
function lcs(a, b): String {
var aSub = a.Substring(0, a.Length-1);
var bSub = b.Substring(0, b.Length-1);
if (a.Length == 0 || b.Length == 0) {
return "";
} else if (a[a.Length-1] == b[b.Length-1]) {
return lcs(aSub, bSub) + a[a.Length-1];
} else {
var x = lcs(a, bSub);
var y = lcs(aSub, b);
return (x.Length > y.Length) ? x : y;
}
}
However, when I run it, Unity gives me a negative number exception on the second line. On modifying it to say a.Length, Unity crashed. Can someone tell me if this is a problem with Unity handling recursive functions or is my code flawed somewhere.
I essentially have a=“start” and b=“startblue1stepbgreen2stepgred1stepryellow2stepy”. Thus, the function should return “start”.
THanks!