Anonymous user
Longest common subsequence: Difference between revisions
→Recursion: Calculate len(a) and len(b) once each and reuse the value
(→{{header|D}}: ++ fortran) |
(→Recursion: Calculate len(a) and len(b) once each and reuse the value) |
||
Line 268:
This is not a particularly fast algorithm, but it gets the job done eventually. The speed is a result of many recursive function calls.
<lang java>public static String lcs(String a, String b){
int bLen = b.length();
if(aLen == 0 || bLen == 0){
return "";
}else if(a.charAt(
return lcs(a.substring(0,
+ a.charAt(
}else{
String x = lcs(a, b.substring(0,
String y = lcs(a.substring(0,
return (x.length() > y.length()) ? x : y;
}
|