cbangchen
11/11/2018 - 7:40 AM

14. Longest Common Prefix - DifficultyEasy - 2018.8.20

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.size() <= 0)
            return "";
        string result = "";
        bool stop = false;
        char comparedChar;
        for (int i = 0; ; i++) {
            comparedChar = strs[0][i];
            for (int j = 0; j < strs.size(); j++) {
                if (strs[j].size() <= i || strs[j][i] != comparedChar) {
                    stop = true;
                    break;
                }
            }
            if (stop) break;
            result += comparedChar;
        }
        return result;
    }
};