Short accepted cpp solution


  • 0
    T
     string longestCommonPrefix(vector<string> &strs) {
            string prefix = "";
            if(strs.empty()) return prefix;
            for(int j = 0; j < strs[0].length(); ++j)
            {
                for(int i = 1; i < strs.size(); ++i){
                    if(j >= strs[i].size() || strs[i][j] != strs[0][j])
                       return prefix;
                }
                prefix += strs[0][j];
            }
            return prefix;
        }

Log in to reply
 

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