C++ sol with O(n^2) complexity


#1

string Solution::longestCommonPrefix(vector &A) {
int k=0;
int count=1;

string ans="";

if(A.size()==1)
    {ans=A[0]; return ans;}
while(1)
{
    char ch;
    count=1;
    for(int j=1;j<A.size();j++)
    {
        string s1=A[j-1];
        string s2=A[j];
        if(s1[k]==s2[k])
            {ch=s1[k];count++;}
        else
            break;
    }
    if(count==A.size())
        ans+=ch;
    else
        break;
        
    k++;
}


return ans;

}