Here you can find the source of LCS(String input1, String input2)
public static void LCS(String input1, String input2)
//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(); } }