Skip to content

Commit

Permalink
day 11 (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spreeha authored and MadhavBahl committed Jan 3, 2019
1 parent 01da629 commit dd79ee7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
45 changes: 45 additions & 0 deletions day11/Java/longestCommonSubstring.java
@@ -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);
}
}
46 changes: 45 additions & 1 deletion day11/README.md
Expand Up @@ -89,7 +89,6 @@ console.log(longestSubstring ("abcdefg", "xyabcz"));
console.log(longestSubstring ("XYXYXYZ", "XYZYX"));
```


## C++ Implementation

### [Solution using dynamic programming](./C++/longestCommonSubstringday11.cpp)
Expand Down Expand Up @@ -148,3 +147,48 @@ int main() {
cout << "len : " << ma << " is " << ans;
}
```

## Java Implementation

### [Solution](./Java/longestCommonSubstring.java)

```java
/**
* @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);
}
}
```

0 comments on commit dd79ee7

Please sign in to comment.