Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day 11 #117

Merged
merged 2 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions day11/Java/longestCommonSubstring.java
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);
}
}
46 changes: 45 additions & 1 deletion day11/README.md
Original file line number Diff line number Diff line change
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);
}
}
```