mercredi 14 décembre 2022

What is the time complexity of the following function? [closed]

string longestCommonPrefix(vector<string>& strs) {
    if (strs.empty()) 
    {
        return "";
    }

    for (int i = 1; i < strs.size(); ++i)
    {
        while (strs[i].find(strs[0]) != 0)
        {
            strs[0].pop_back();

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

    return strs[0];
}

I want to analyze the efficiency of the algorithm. I think that the auxiliary space complexity is O(1) but I couldn't figure out time complexity of the function. What is its time complexity?

Thanks.

Aucun commentaire:

Enregistrer un commentaire