Java LCS LCS(String input1, String input2)

Here you can find the source of LCS(String input1, String input2)

Description

LCS

License

Open Source License

Declaration

public static void LCS(String input1, String input2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static void LCS(String input1, String input2) {
        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 {
                    lcs[i][j] = Math.max(lcs[i + 1][j], lcs[i][j + 1]);
                }/*from   w w  w  . j a v  a  2s. c o m*/
            }
        }
        int i = 0, j = 0;
        while (i < M && j < N) {
            if (input1.charAt(i) == input2.charAt(j)) {
                System.out.print(input1.charAt(i));
                i++;
                j++;
            } else if (lcs[i + 1][j] >= lcs[i][j + 1]) {
                i++;
            } else {
                j++;
            }
        }
        System.out.println();
    }
}

Related

  1. LCS(String a, String b)
  2. lcs(String a, String b)
  3. LCS(String A, String B)
  4. lcs(String a, String b)
  5. lcs(String arg0, String arg1)
  6. lcs(String s1, int s1min, int s1max, String s2, int s2min, int s2max)
  7. lcs(String s1, String s2)
  8. lcs(String str1, String str2)
  9. lcs(String X, String Y, int m, int n)