Easy Peasy C++ Code


#1
OK    
string Solution::longestCommonPrefix(vector<string> &A) {
        string ans = A[0];
        int n = A.size();
        for (int i = 1; i < n; i++) {
            int curr = min(ans.size(), A[i].size());
            while (ans.substr(0, curr) != A[i].substr(0, curr)) {
                curr--;
            }
            if (curr < 0) curr = 0;
            ans = ans.substr(0, curr);
        }
        return ans;
    }