leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

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

Open ExploiTR opened this issue 1 month ago • 0 comments

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.

ExploiTR avatar Nov 07 '25 04:11 ExploiTR