clueless coding // TODO: be smarter

LeetCode Longest Common Prefix: CPP Solution



TL;DR Code Solution


string longestCommonPrefix(vector<string>& strs) {

    if (strs.size() == 0 || strs[0].size() == 0) {
        return "";
    }

    string result = "";
    for (int i = 0; i < strs[0].size(); i++) {
        char current = strs[0][i];
        for (int j = 1; j < strs.size(); j++) {
            if (strs[j][i] != current) {
                return result;   
            }
        }
        result += string(1, current);
    }
    return result;

}