Different Solution from Editorial in C++ | Divide and Conquer


#1
string solve(vector<string> &A, int low, int high){
    if(low==high) return A[low];
    if(high == (low + 1)){
        string res = "";
        for(int i=0; i<min(A[low].length(), A[high].length()); i++){
            if(A[low][i]==A[high][i]) res += A[low][i];
            else break;
        }
        return res;
    }
    int mid = low + (high - low)/2;
    string s1 = solve(A, low, mid);
    string s2 = solve(A, mid + 1, high);
    string res = "";
    for(int i=0; i<min(s1.length(), s2.length()); i++){
        if(s1[i]==s2[i]) res += s1[i];
        else break;
    }
    return res;
}
string Solution::longestCommonPrefix(vector &amp;A) {
    return solve(A, 0, A.size()-1);
}