Write a function to find the longest common prefix string amongst an array of strings.
code
public class LongestCommonPrefix {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return null;
}
String prefix = strs[0];
for (int i = 0; i < strs.length; i++) {
int j = 0;
while (j < strs[i].length() && j < prefix.length()
&& strs[i].charAt(j) == prefix.charAt(j)) {
j++;
}
if (j == 0) {
return "";
}
prefix = prefix.substring(0, j);
}
return prefix;
}
}