-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstring.java
More file actions
59 lines (50 loc) · 1.71 KB
/
Copy pathSubstring.java
File metadata and controls
59 lines (50 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class LongestCommonSubstring {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
boolean quit=false;
System.out.println("Enter the Strings, press 0 to exit");
ArrayList<String> list=new ArrayList<>();
String smallest=scanner.next();
list.add(smallest);
while(!quit) {
String s = scanner.next();
if (s.equals("0")) {
quit = true;
} else {
list.add(s);
if (s.length() < smallest.length()) {
smallest = s;
System.out.println(smallest);
}
}
}
String stem="";
ArrayList<String> common=new ArrayList<>();
for(int i=0;i<smallest.length();i++){
for(int j=i+1;j<=smallest.length();j++){
String subString=smallest.substring(i,j);
boolean contains=true;
for(int k=0;k<list.size();k++){
if(!list.get(k).contains(subString)){
contains=false;
break;
}
}
if(contains){
if(stem.length()<subString.length()){
stem=subString;
common.clear();
common.add(stem);
}else if(stem.length()==subString.length()){
common.add(subString);
}
}
}
}
Collections.sort(common);
System.out.println(common.get(0));
}
}