Java Program to Implement Longest Common Subsequence Algorithm

«
»
This is a Java Program to Implement Longest Common Subsequence Algorithm. This program finds the longest common subsequence between two strings.

Here is the source code of the Java Program to Implement Longest Common Subsequence Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to implement Longest Common Subsequence Algorithm
  3.  **/
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.io.IOException;
  8.  
  9. /** Class  LongestCommonSubsequence **/
  10. public class  LongestCommonSubsequence
  11. {    
  12.     /** function lcs **/
  13.     public String lcs(String str1, String str2)
  14.     {
  15.         int l1 = str1.length();
  16.         int l2 = str2.length();
  17.  
  18.         int[][] arr = new int[l1 + 1][l2 + 1];
  19.  
  20.         for (int i = l1 - 1; i >= 0; i--)
  21.         {
  22.             for (int j = l2 - 1; j >= 0; j--)
  23.             {
  24.                 if (str1.charAt(i) == str2.charAt(j))
  25.                     arr[i][j] = arr[i + 1][j + 1] + 1;
  26.                 else 
  27.                     arr[i][j] = Math.max(arr[i + 1][j], arr[i][j + 1]);
  28.             }
  29.         }
  30.  
  31.         int i = 0, j = 0;
  32.         StringBuffer sb = new StringBuffer();
  33.         while (i < l1 && j < l2) 
  34.         {
  35.             if (str1.charAt(i) == str2.charAt(j)) 
  36.             {
  37.                 sb.append(str1.charAt(i));
  38.                 i++;
  39.                 j++;
  40.             }
  41.             else if (arr[i + 1][j] >= arr[i][j + 1]) 
  42.                 i++;
  43.             else
  44.                 j++;
  45.         }
  46.         return sb.toString();
  47.     }
  48.  
  49.     /** Main Function **/
  50.     public static void main(String[] args) throws IOException
  51.     {    
  52.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  53.         System.out.println("Longest Common Subsequence Algorithm Test\n");
  54.  
  55.         System.out.println("\nEnter string 1");
  56.         String str1 = br.readLine();
  57.  
  58.         System.out.println("\nEnter string 2");
  59.         String str2 = br.readLine();
  60.  
  61.         LongestCommonSubsequence obj = new LongestCommonSubsequence(); 
  62.         String result = obj.lcs(str1, str2);
  63.  
  64.         System.out.println("\nLongest Common Subsequence : "+ result);
  65.     }
  66. }

Longest Common Subsequence Algorithm Test
 
 
Enter string 1
longestcommonsubsequence
 
Enter string 2
dynamicprogramming
 
Longest Common Subsequence : ncommn
 
 
 
 
 
Longest Common Subsequence Algorithm Test
 
 
Enter string 1
alphabetagamma
 
Enter string 2
applemangopineapple
 
Longest Common Subsequence : apeaga

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.