-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLcsSolution.java
41 lines (39 loc) · 1.09 KB
/
LcsSolution.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
public class LcsSolution {
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
public static String longestCommonPrefix(String[] strs) {
// write your code here
if(strs == null){
System.out.println("NULL ARGUMENT");
return null;
}
String result = "";
int minLen = 256;
int i ;
int j ;
for(String s : strs){
minLen = minLen < s.length() ? minLen : s.length();
}
if(minLen == 0){
System.out.println(minLen);
return result;
}
lable:for( i = 0 ; i < minLen ;i++){
for( j = 0 ;j < strs.length - 1 ; j++){
if(strs[j].charAt(i) == strs[j+1].charAt(i)){
continue;
}
else{
break lable;
}
}
}
return strs[0].substring(0,i);
}
public static void main(String args[]){
String[] a = {};
System.out.println(longestCommonPrefix(a));
}
}