Write a function to find the longest common prefix string amongst an array of strings.
Have you met this question in a real i
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
int len = strs.length;
if (len < 2) {
return strs[0];
} else {
String result = strs[0];
for (int i = 1; i < strs.length; i++) {
if (result.length() > strs[i].length()) {
result = strs[i];
}
}
int lenCh = result.length();
for (int i = 0; i < lenCh; i++) {
for (int j = 0; j < len; j++) {
if (result.charAt(i) != strs[j].charAt(i)) {
return result.substring(0, i);
}
}
}
return result;
}
}
}