```
public String longestCommonPrefix(String[] strs) {
if (strs.length == 0) return "";
String first = strs[0];
int end = first.length();
for (int i = 1; i < strs.length; i++) {
int j = 0;
while (j < Math.min(first.length(), strs[i].length()) && first.charAt(j) == strs[i].charAt(j)) {
j++;
}
end = Math.min(j, end);
}
return first.substring(0, end);
}
```