Java Program to Implement Longest Common Substring Algorithm

This is a Java Program to implement Longest Common Substring Algorithm. This program finds the longest common substring between two strings.

Here is the source code of the Java Program to implement Longest Common Substring 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 Substring Algorithm
  3.  **/
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.io.IOException;
  8.  
  9. /** Class  LongestCommonSubstring **/
  10. public class  LongestCommonSubstring
  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.         int len = 0, pos = -1;
  20.  
  21.         for (int x = 1; x < l1 + 1; x++)
  22.         {
  23.             for (int y = 1; y < l2 + 1; y++)
  24.             {
  25.                 if (str1.charAt(x - 1) == str2.charAt(y - 1))
  26.                 {
  27.                         arr[x][y] = arr[x - 1][y - 1] + 1;
  28.                         if (arr[x][y] > len)
  29.                         {
  30.                             len = arr[x][y];
  31.                             pos = x;
  32.                         }               
  33.                 }
  34.                 else
  35.                     arr[x][y] = 0;
  36.             }
  37.         }        
  38.  
  39.         return str1.substring(pos - len, pos);
  40.     }
  41.  
  42.     /** Main Function **/
  43.     public static void main(String[] args) throws IOException
  44.     {    
  45.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  46.         System.out.println("Longest Common Substring Algorithm Test\n");
  47.  
  48.         System.out.println("\nEnter string 1");
  49.         String str1 = br.readLine();
  50.  
  51.         System.out.println("\nEnter string 2");
  52.         String str2 = br.readLine();
  53.  
  54.         LongestCommonSubstring obj = new LongestCommonSubstring(); 
  55.         String result = obj.lcs(str1, str2);
  56.  
  57.         System.out.println("\nLongest Common Substring : "+ result);
  58.     }
  59. }

Longest Common Substring Algorithm Test
 
 
Enter string 1
abcdefghijklmnopqrstuvwxyz
 
Enter string 2
randomijklmnstring
 
Longest Common Substring : ijklmn

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.