-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
31 lines (29 loc) · 1006 Bytes
/
LongestCommonPrefix.java
File metadata and controls
31 lines (29 loc) · 1006 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
import java.util.Arrays;
import java.util.Optional;
/*
* https://leetcode.com/problems/longest-common-prefix/
*/
public class LongestCommonPrefix {
public String longestCommonPrefix(String[] strs) {
Optional<Integer> maybeMinLength = Arrays.stream(strs).map(String::length).min(Integer::compareTo);
int minLength = maybeMinLength.orElse(0);
if (minLength == 0) {
return "";
}
int flag = minLength;
for (int i = 1; i <= minLength; i++) {
String prefix = strs[0].substring(0, i);
if (!Arrays.stream(strs).allMatch(s -> s.startsWith(prefix))) {
flag = i - 1;
System.out.println(flag);
break;
}
}
return strs[0].substring(0, flag);
}
public static void main(String[] args) {
System.out.println(new LongestCommonPrefix().longestCommonPrefix(
new String[]{"flower", "flow", "flight"}
)); // "fl"
}
}