8ms c++ solution, easy to understand


  • 0
    M
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.empty()) return "";
        for(int idx = 0; strs[0][idx] != '\0'; ++idx)
        {
            for(auto& str : strs ) 
                if(str[idx] != strs[0][idx]) return strs[0].substr(0,idx);
        }
        return strs[0];
    }

Log in to reply
 

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