-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestCommonPrefixLC.java
More file actions
40 lines (32 loc) · 980 Bytes
/
longestCommonPrefixLC.java
File metadata and controls
40 lines (32 loc) · 980 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
32
33
34
35
36
37
38
39
40
# https://leetcode.com/problems/longest-common-prefix/
class longestCommonPrefixLC {
public String longestCommonPrefix(String[] strs) {
if(strs.length==0)
return "";
String commonPrefix = "";
int minLength = 100000;
for(int i=0;i<strs.length;i++){
if(strs[i].length() < minLength){
minLength = strs[i].length();
}
}
for(int i=0; i < minLength;i++){
char check = 0;
Boolean common = true;
for(int j=0;j<strs.length;j++){
if(j==0){
check = strs[j].charAt(i);
}
else if(strs[j].charAt(i) != check) {
common = false;
}
}
if(common == false){
break;
}else{
commonPrefix = commonPrefix + check;
}
}
return commonPrefix;
}
}