-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongPrefix.java
41 lines (36 loc) · 1.04 KB
/
LongPrefix.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
package JAVA.LEETCODE;
public class LongPrefix {
public static void main(String[] args) {
String [] strs = {"reflower","flow","flight"};
System.out.println(solution(strs));
}
static String solution(String [] strs){
int min = strs[0].length();
String minStr = strs[0];
for(String data : strs){
if(data.length() < min){
min = data.length();
minStr = data;
}
}
String lpx = "";
boolean end = false;
for(int i=0;i<=minStr.length()-1;i++){
int k = 0;
String match = String.valueOf(minStr.charAt(i));
while (k<strs.length){
if(!match.equals(strs[k].split("")[i])){
end = true;
break;
}
k+=1;
}
if(end){
break;
}else{
lpx = lpx + match;
}
}
return lpx;
}
}