[Link] https://leetcode.com/problems/longest-common-prefix/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
class Solution {
public String longestCommonPrefix(String[] strs) {
String s = strs[0];
int idx = strs[0].length() - 1;
for(int i = 1; i < strs.length; i++) {
idx = Math.min(strs[i].length() - 1, idx);
for(int j = 0; j <= idx; j++) {
if(s.charAt(j) != strs[i].charAt(j)) {
idx = j - 1;
break;
}
}
}
return s.substring(0, idx + 1);
}
}