Clean C++ solution - easy understanding


  • 0
    Y

    class Solution {
    public:

    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty()) return "";
        string str1 = strs[0];
        if (str1 == "") return "";
        
        for (int i = 1; i < strs.size(); ++i) {
            string str2 = strs[i];
            if (str2 == "") return "";
            
            int ind = 0;
            while (str1[ind] == str2[ind]) ind++;
            str1 = str1.substr(0, ind);
        }
        
        return str1;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.