Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
public class LongestCommonSubstring {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(lcs("testing123testing", "thisisatest"));
|
||||
System.out.println(lcs("test", "thisisatest"));
|
||||
System.out.println(lcs("testing", "sting"));
|
||||
System.out.println(lcs("testing", "thisisasting"));
|
||||
}
|
||||
|
||||
static String lcs(String a, String b) {
|
||||
if (a.length() > b.length())
|
||||
return lcs(b, a);
|
||||
|
||||
String res = "";
|
||||
for (int ai = 0; ai < a.length(); ai++) {
|
||||
for (int len = a.length() - ai; len > 0; len--) {
|
||||
|
||||
for (int bi = 0; bi <= b.length() - len; bi++) {
|
||||
|
||||
if (a.regionMatches(ai, b, bi, len) && len > res.length()) {
|
||||
res = a.substring(ai, ai + len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue