-
Notifications
You must be signed in to change notification settings - Fork 2
/
LCS.java
35 lines (31 loc) · 1.17 KB
/
LCS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class LCS {
public static String lcs(String x, String y) {
// dynanmic programming problem!
// start at the end
// the lcs is the longest lcs fo the substring + 1 if these guys match..
// do we shorten x or y?
// if they match we shorten x and y
// if they dont we shorten x
if (x.equals("") || y.equals("")) {
return "";
}
if (x.equals(y)) {
return x;
}
String maxString = "";
String maxSubstring = "";
if (x.charAt(x.length() - 1) == y.charAt(y.length() - 1)) {
maxSubstring = lcs(x.substring(0, x.length() - 1), y.substring(0, y.length() - 1)) + x.charAt(x.length() - 1);
maxString = maxSubstring.length() > maxString.length() ? maxSubstring : maxString;
}
if (x.length() > 1) {
maxSubstring = lcs(x.substring(0, x.length() - 1), y);
maxString = maxSubstring.length() > maxString.length() ? maxSubstring : maxString;
}
if (y.length() > 1) {
maxSubstring = lcs(x, y.substring(0, y.length() - 1));
maxString = maxSubstring.length() > maxString.length() ? maxSubstring : maxString;
}
return maxString;
}
}