Java Utililty Methods LCS

List of utility methods to do LCS

Description

The list of methods to do LCS are organized into topic(s).

Method

Stringlcs(char[] A, char[] B)
lcs
if (A == null || B == null)
    return null;
final int n = A.length;
final int m = B.length;
if (n == 0 || m == 0)
    return null;
int[][] dp = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
...
intlcs(char[] inputString1, char[] inputString2, int length1, int length2)
lcs
if (length1 == 0 || length2 == 0)
    return 0;
if (inputString1[length1 - 1] == inputString2[length2 - 1])
    return 1 + lcs(inputString1, inputString2, length1 - 1, length2 - 1);
else
    return Math.max(lcs(inputString1, inputString2, length1, length2 - 1),
            lcs(inputString1, inputString2, length1 - 1, length2));
intLCS(String a, String b)
LCS
int Rows = a.length() + 1;
int Cols = b.length() + 1;
dp = new int[Rows][Cols];
for (int i = 0; i <= a.length(); i++) {
    dp[i][0] = 0;
for (int i = 0; i <= b.length(); i++) {
    dp[0][i] = 0;
...
intlcs(String a, String b)
lcs
final int m = a.length();
final int n = b.length();
int[] v0 = new int[n];
int[] v1 = new int[n];
int z = 0;
for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
        if (a.charAt(i) == b.charAt(j)) {
...
intLCS(String A, String B)
LCS
if (A.length() == 0 || B.length() == 0) {
    return 0;
int lenA = A.length();
int lenB = B.length();
if (A.charAt(lenA - 1) == B.charAt(lenB - 1)) {
    return 1 + LCS(A.substring(0, lenA - 1), B.substring(0, lenB - 1));
} else {
...
Stringlcs(String a, String b)
lcs
int[][] lengths = new int[a.length() + 1][b.length() + 1];
for (int i = 0; i < a.length(); i++)
    for (int j = 0; j < b.length(); j++)
        if (a.charAt(i) == b.charAt(j))
            lengths[i + 1][j + 1] = lengths[i][j] + 1;
        else
            lengths[i + 1][j + 1] = Math.max(lengths[i + 1][j], lengths[i][j + 1]);
StringBuffer sb = new StringBuffer();
...
longlcs(String arg0, String arg1)
lcs
if (arg0 == null || arg1 == null) {
    return 0;
return lcs(arg0, arg1, 0, 0);
voidLCS(String input1, String input2)
LCS
int M = input1.length();
int N = input2.length();
int[][] lcs = new int[M + 1][N + 1];
for (int i = M - 1; i >= 0; i--) {
    for (int j = N - 1; j >= 0; j--) {
        if (input1.charAt(i) == input2.charAt(j)) {
            lcs[i][j] = lcs[i + 1][j + 1] + 1;
        } else {
...
intlcs(String s1, int s1min, int s1max, String s2, int s2min, int s2max)
lcs
if (s1min < 0 || s1max > s1.length() || s1max - s1min < 1 || s2min < 0 || s2max > s2.length()
        || s2max - s2min < 1) {
    return 0;
int array[][] = new int[s1max - s1min + 1][s2max - s2min + 1];
for (int i = 1; i < array.length; i++) {
    for (int j = 1; j < array[i].length; j++) {
        if (s1.charAt(s1min + i - 1) == s2.charAt(s2min + j - 1)) {
...
Stringlcs(String s1, String s2)
lcs
int l1 = s1.length();
int l2 = s2.length();
int[][] val = new int[l1 + 1][l2 + 1];
int[][] dir = new int[l1 + 1][l2 + 1];
for (int i = 0; i <= l2; i++) {
    val[0][i] = 0;
    dir[0][i] = 2;
for (int i = 0; i <= l1; i++) {
    val[i][0] = 0;
    dir[i][0] = 1;
int tVal, tDir;
for (int i = 1; i <= l1; i++) {
    for (int j = 1; j <= l2; j++) {
        tVal = val[i - 1][j];
        tDir = 1;
        if (val[i][j - 1] > tVal) {
            tVal = val[i][j - 1];
            tDir = 2;
        if (s1.charAt(i - 1) == s2.charAt(j - 1) && val[i - 1][j - 1] + 1 > tVal) {
            tVal = val[i - 1][j - 1] + 1;
            tDir = 3;
        val[i][j] = tVal;
        dir[i][j] = tDir;
return getSolution(dir, s1, s2);