Skip to content

Bug Report for longest-common-prefix [TEST Case Issue] #4970

@ExploiTR

Description

@ExploiTR

Bug Report for https://neetcode.io/problems/longest-common-prefix

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string last = strs[0];
        int checkSize = last.size();
        
        for(const auto& str : strs)
            for(int i = 0; i<checkSize; i++)
                if(str[i] != last[i])
                    checkSize = i;

        return last.substr(0,checkSize);
    }
};

This code shouldn't pass submission, it has a bug.
Suppose testcase : ["flower", "fl"] -> here checkSize is larger than str[i] for i =1.

But this code is getting accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions