-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (28 loc) · 766 Bytes
/
Solution.java
File metadata and controls
31 lines (28 loc) · 766 Bytes
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
package leetCode_14;
/**
* @author dimdark
* @date 2017-05-02
* @time 11:00 PM
*/
public class Solution {
public String longestCommonPrefix(String[] s) {
if(s==null || s.length==0){
return "";
}
int minLen=s[0].length(),minIndex=0;
for(int i=1; i<s.length; ++i){
if(minLen>s[i].length()){
minLen = s[i].length();
minIndex = i;
}
}
String result = s[minIndex];
for(int i=0; i<s.length; ++i){
while(s[i].indexOf(result)!=0){ //not the common prefix
result = result.substring(0,result.length()-1);
}
if(result.length()==0) break;
}
return result.toString();
}
}