Skip to content

Commit 433183f

Browse files
authored
Create 14. Longest Common Prefix
1 parent 0e76415 commit 433183f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

14. Longest Common Prefix

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Solution {
2+
public String longestCommonPrefix(String[] strs) {
3+
if (strs==null || strs.length==0){
4+
return "";
5+
}
6+
7+
String prefix = strs[0];
8+
for(int i = 0; i< strs.length;i++){
9+
int j = 0;
10+
while(j<strs[i].length() && j<prefix.length()&& strs[i].charAt(j)== strs[0].charAt(j)){
11+
j++;
12+
}
13+
if(j==0){
14+
return "";
15+
}
16+
prefix = prefix.substring(0,j);
17+
}
18+
19+
return prefix;
20+
}
21+
}

0 commit comments

Comments
 (0)