5 lines of code O(n*min.length()) in C++


#1
string Solution::longestCommonPrefix(vector<string> &A) {
    if(A.size()==1) return A[0];
    for(int i=0;i<A[0].size();i++){
        for(int j=0;j<A.size()-1;j++){
            if(A[j][i]==A[j+1][i]) continue;
            else return A[j].substr(0,i);
        }
    }

}


#2

I think your code fails for this testcase.
[“ab”,“abc”,“abcd”]