[LeetCode]14. Longest Common Prefix

原题链接:https://leetcode.com/problems/longest-common-prefix/description/

[LeetCode]14. Longest Common Prefix

我的实现:

class Solution {
public:
    string findCommonPrefix(string s1, string s2) {
        string common_prefix = "";
        for (int i = 0; i < s1.length() && i < s2.length(); i++) {
            if (s1[i] != s2[i])
                return common_prefix;
            else
                common_prefix += s1[i];
        }
        return common_prefix;
    }
    
    string longestCommonPrefix(vector<string>& strs) {
        string common_prefix = "";
        if (strs.size() > 0)
            common_prefix = strs[0];
        for (int i = 1; i < strs.size(); i++) {
            common_prefix = findCommonPrefix(strs[i], common_prefix);
        }
        return common_prefix;
    }
};