Simple C++ solution 2


#1

string Solution::longestCommonPrefix(vector &A) {

string res;
if (!A.size())
    return res;
string prev = A[0];
int end = prev.size() - 1;
for (int i = 1; i < A.size(); i++) {
    string curr = A[i];
    for (int j = 0; j <= end; j++) {
        if (prev[j] != curr[j]) {
            if (j == 0)
                return res;
            end = j - 1;
            break;
        }
    }
}

for (int k = 0; k <= end; k++)
    res += prev[k];
    
return res;

}