Very Very Easy Solution In C++


#1

string Solution::longestCommonPrefix(vector &A) {
int n=A.size();
string x;
x=A[0];
int i=1;
while(i<n){
for(int j=0;j<x.length();j++){
if(x[j]!=A[i][j]){
x.erase(j);
break;
}
}
i++;
}
return x;
}


#2

Can you explain the functioning of erase() function.
x.erase(j) - erase the charactor present at jth position or erase all the charactor present at and beyond the jth position.


#3

erase the character present at jth position