You gonna like this one O(n*x) solution where x is the length of prefix string


#1

string Solution::longestCommonPrefix(vector &A) {
sort(A.begin(),A.end());
string res="";
string temp=A[0];
for(int i=1;i<A.size();i++)
{
int j=0;
string res="";
while(temp[j]==A[i][j])
{
res.push_back(temp[j++]);
}
temp=res;
}
return temp;
}