-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day-9.java
36 lines (34 loc) · 1.03 KB
/
Day-9.java
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
import java.awt.*;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.function.Function.identity;
class Solution {
public String longestCommonPrefix(String[] strs) {
StringBuilder res = new StringBuilder();
if (strs.length == 0) {
return "";
}else if(strs.length == 1){
return strs[0];
}
Optional value = Arrays.stream(strs).filter(str -> str == null || str.length() == 0).findAny();
if (value.isPresent())
return "";
int i = 0;
while (i<strs[0].length()) {
char val = strs[0].charAt(i);
for (String str : strs) {
if (str.length()<=i ||val != str.charAt(i)) {
return res.toString();
}
}
res.append(val);
i++;
}
return res.toString();
}
}