C++


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

Log in to reply
 

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