-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM84.java
More file actions
28 lines (27 loc) · 807 Bytes
/
BM84.java
File metadata and controls
28 lines (27 loc) · 807 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
package NiukeTOP101;
public class BM84 {
public String longestCommonPrefix (String[] strs) {
if(strs.length == 0){
return "";
}
StringBuilder sb = new StringBuilder();
int minLength = 5010;
for (int i = 0; i < strs.length; i++) {
if(strs[i].length() < minLength){
minLength = strs[i].length();
}
}
boolean flag = true;
for (int i = 0; i < minLength && flag; i++) {
for (int j = 1; j < strs.length; j++) {
if(strs[j].charAt(i) != strs[0].charAt(i)){
flag = false;
break;
}
}
if (flag)
sb.append(strs[0].charAt(i));
}
return sb.toString();
}
}