What's wrong in the code


#1

What is wrong in this code, I’m getting error that the output doesn’t matches with the expected one.
string Solution::longestCommonPrefix(vector &A) {
int n=A.size();
int min=A[0].length();
for(int i=1;i<n;i++)
{
if(A[i].length()<min)
min=A[i].length();
}
string ans="";
char current;
for(int j=0;j<min;j++)
{
current=A[0][j];
for(int k=1;k<n;k++)
{
if(A[k][j]!=current)
{
return ans;
}
else
{
ans+=current;
}
}

}
return (ans);

}


#2

Modify your code as following:
int n=A.size();
int min=A[0].length();
for(int i=1;i<n;i++)
{
if(A[i].length()<min)
min=A[i].length();
}
string ans="";
char current;
for(int j=0;j<min;j++)
{
current=A[0][j];
for(int k=1;k<n;k++)
{
if(A[k][j]!=current)
{
return ans;
}
}
ans+=current;
}
return (ans);