-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestCommonString.java
58 lines (48 loc) · 1.65 KB
/
longestCommonString.java
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
import java.util.Scanner;
import com.oracle.webservices.internal.api.databinding.Databinding.Builder;
public class longestCommonString {
public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0) {
return "";
}
if (strs.length == 1) {
return strs[0];
}
char[] prefix = strs[0].toCharArray();
String foundStr = "";
boolean found = false;
StringBuilder buil = new StringBuilder();
int i = 0;
while (i < prefix.length && !found) {
int j = i;
while (j < prefix.length) {
buil.append(prefix[j]);
String aux = buil.toString();
found = checkStrings(aux, strs, strs[0]);
if (found && foundStr.length() < aux.length())
if(aux.contains(foundStr) || foundStr.isEmpty() ) foundStr = aux;
j++;
}
buil.setLength(0);
found=true;
i++;
}
return foundStr;
}
public static boolean checkStrings(String str, String[] strs, String prefix) {
boolean found = false;
int i = 1;
while (i < strs.length && !found) {
String aux = strs[i];
if (!aux.contains(str) || prefix.indexOf(str) != aux.indexOf(str)) found = true;
i++;
}
return !found;
}
public static void main(String[] args) {
// keep this function call here
// Scanner s = new Scanner(System.in);
String[] str = new String[] {"babb","caa"};
System.out.print(longestCommonPrefix(str));
}
}