7-line C++ Solution, Easy to Understand


  • 0
    class Solution {
    public:
        string longestCommonPrefix(vector<string>& strs) {
            if(strs.size()==0)
                return "";
            for(int index=0;index<strs[0].size();index++)
                for(int i=1;i<strs.size();i++)
                    if(strs[i][index]!=strs[0][index])
                        return strs[0].substr(0,index);
            return strs[0];
        }
    };
    

Log in to reply
 

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