-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
27 lines (24 loc) · 828 Bytes
/
Copy pathSolution.java
File metadata and controls
27 lines (24 loc) · 828 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
class Solution {
public String longestCommonPrefix(String[] strs) {
String prefix = null;
for (String str : strs) {
if (prefix == null) {
prefix = str;
} else {
String tempPrefix = "";
for (int j = 0; j < Math.min(prefix.length(), str.length()); j++) {
if (prefix.charAt(j) == str.charAt(j)) {
tempPrefix += new String(new char[]{prefix.charAt(j)});
} else {
break;
}
}
prefix = tempPrefix;
if (prefix.equals("")) {
return prefix;
}
}
}
return prefix == null ? "" : prefix;
}
}