Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01da629
commit dd79ee7
Showing
2 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package javaapplication3; | ||
|
||
/** | ||
* @date 03/01/1998 | ||
* @author SPREEHA DUTTA | ||
*/ | ||
import java.util.*; | ||
public class longestCommonSubstring { | ||
static int max=0;static String sm; | ||
public static void generate(String s,String st) | ||
{ | ||
String str; | ||
for(int i=0;i<=s.length()-1;i++) | ||
{ | ||
for(int j=i+1;j<=s.length();j++) | ||
{ | ||
str=s.substring(i,j); | ||
if(st.contains(str)) | ||
{ | ||
if(max<str.length()) | ||
{ | ||
max=str.length(); | ||
sm=str; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
public static void main(String []args) | ||
{ | ||
Scanner sc=new Scanner(System.in); | ||
String s1,s2; | ||
System.out.println("Enter the two strings "); | ||
s1=sc.next(); | ||
s2=sc.next(); | ||
generate(s1,s2); | ||
generate(s2,s1); | ||
System.out.println("Longest common substring is "+sm); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters