Write a function to find the longest common prefix string amongst an array of strings.
Subscribe to see which companies asked this question
public class Solution {
public String longestCommonPrefix(String[] strs) {
int n = strs.length;
if(n==0)return "";
public class Solution {
public String longestCommonPrefix(String[] strs) {
int n = strs.length;
if (n == 0)
return "";
int minlen = strs[0].length();
for (int i = 1; i < n; i++) {
if (strs[i].length() < minlen)
minlen = strs[i].length();
}
char test = '0';
int last = minlen;
if (minlen == 0)
return "";
outer: for (int i = 0; i < minlen; i++) {
test = strs[0].charAt(i);
for (int j = 1; j < n; j++) {
if (test != strs[j].charAt(i)) {
last = i;
break outer;
}
}
}
return strs[0].substring(0, last);
}
}