File tree 2 files changed +90
-1
lines changed 2 files changed +90
-1
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+ package javaapplication3 ;
7
+
8
+ /**
9
+ * @date 03/01/1998
10
+ * @author SPREEHA DUTTA
11
+ */
12
+ import java .util .*;
13
+ public class longestCommonSubstring {
14
+ static int max =0 ;static String sm ;
15
+ public static void generate (String s ,String st )
16
+ {
17
+ String str ;
18
+ for (int i =0 ;i <=s .length ()-1 ;i ++)
19
+ {
20
+ for (int j =i +1 ;j <=s .length ();j ++)
21
+ {
22
+ str =s .substring (i ,j );
23
+ if (st .contains (str ))
24
+ {
25
+ if (max <str .length ())
26
+ {
27
+ max =str .length ();
28
+ sm =str ;
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ public static void main (String []args )
35
+ {
36
+ Scanner sc =new Scanner (System .in );
37
+ String s1 ,s2 ;
38
+ System .out .println ("Enter the two strings " );
39
+ s1 =sc .next ();
40
+ s2 =sc .next ();
41
+ generate (s1 ,s2 );
42
+ generate (s2 ,s1 );
43
+ System .out .println ("Longest common substring is " +sm );
44
+ }
45
+ }
Original file line number Diff line number Diff line change @@ -89,7 +89,6 @@ console.log(longestSubstring ("abcdefg", "xyabcz"));
89
89
console .log (longestSubstring (" XYXYXYZ" , " XYZYX" ));
90
90
```
91
91
92
-
93
92
## C++ Implementation
94
93
95
94
### [ Solution using dynamic programming] ( ./C++/longestCommonSubstringday11.cpp )
@@ -148,3 +147,48 @@ int main() {
148
147
cout << "len : " << ma << " is " << ans;
149
148
}
150
149
```
150
+
151
+ ## Java Implementation
152
+
153
+ ### [ Solution] ( ./Java/longestCommonSubstring.java )
154
+
155
+ ``` java
156
+ /**
157
+ * @date 03/01/1998
158
+ * @author SPREEHA DUTTA
159
+ */
160
+ import java.util.* ;
161
+ public class longestCommonSubstring {
162
+ static int max= 0 ;static String sm;
163
+ public static void generate (String s ,String st )
164
+ {
165
+ String str;
166
+ for (int i= 0 ;i<= s. length()- 1 ;i++ )
167
+ {
168
+ for (int j= i+ 1 ;j<= s. length();j++ )
169
+ {
170
+ str= s. substring(i,j);
171
+ if (st. contains(str))
172
+ {
173
+ if (max< str. length())
174
+ {
175
+ max= str. length();
176
+ sm= str;
177
+ }
178
+ }
179
+ }
180
+ }
181
+ }
182
+ public static void main (String []args )
183
+ {
184
+ Scanner sc= new Scanner (System . in);
185
+ String s1,s2;
186
+ System . out. println(" Enter the two strings " );
187
+ s1= sc. next();
188
+ s2= sc. next();
189
+ generate(s1,s2);
190
+ generate(s2,s1);
191
+ System . out. println(" Longest common substring is " + sm);
192
+ }
193
+ }
194
+ ```
You can’t perform that action at this time.
0 commit comments