C++ code with 11 lines ,9 lines respectively


  • 1
    Z

    Solution one: traverse to get lcp

    class Solution {
    public:
        string common(string& a,string& b)
        {
            int i;
            for(i=0;i<a.size()&&i<b.size();i++)
                    if(a[i]!=b[i])break;
            return a.substr(0,i);
        }
        string longestCommonPrefix(vector<string>& strs) {
            if(strs.size()==0)return string();
            string lcp=strs[0];         //lcp longestCommonPrefix
            for(vector<string>::iterator it=strs.begin();it!=strs.end();it++)
                  lcp=common(lcp,*it);
            return lcp;
        }
    };
    

    Solution two: vertical comparison

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

Log in to reply
 

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