Can anybody tell the error in my code


#1

string Solution::longestCommonPrefix(vector &v)
{
int mi=INT_MAX;
if(v.size()==0) return “”;
string c=v[0];

for(int i=1;i<v.size();i++)
{
    int j=0,k=0,a=0;
    while(j<c.size()and k<v[i].size())
    {
        if(c[i]==v[i][k]) a++;
        else break;
        j++;
        k++;
        
    }
    mi=min(mi,a);
}

return c.substr(0,mi);
}


#2

/*I modified your code little bit and i have submitted its working :slight_smile: */
/*mentioning comments where i modified */

string Solution::longestCommonPrefix(vector &v) {
int mi=INT_MAX;
if(v.size()==0) return “”;
string c=v[0];

for(int i=1;i<v.size();i++)
{
int j=0,k=0,a=0;
a=0;
while(j<c.size()and k<v[i].size())
{
if(c[j]==v[i][k]) /*changed i to j */
{a++;}
else{ break;}
j++;
k++;

}
if (mi>a)
{
    mi=a; /*i dont know how min is calculated in c++ using lib*/
}

}

return c.substr(0,mi);
}


#3

thank you shanky
for considering my code