LeetCode-Feedback / LeetCode-Feedback

661 stars 315 forks source link

[BUG] - Incorrect test case for 1071. Greatest Common Divisor of Strings #23185

Closed shawfire closed 1 month ago

shawfire commented 1 month ago

LeetCode Username

shaw_fire

Problem Number, Title, and Link

1071, Greatest Common Divisor of Strings https://leetcode.com/problems/greatest-common-divisor-of-strings

Bug Category

Incorrect test case (Output of test case is incorrect as per the problem statement)

Bug Description

I think this test case is incorrect: str1 = "TAUXXTAUXXTAUXXTAUXXTAUXX" str2 = "TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX" Expected = "TAUXX" In my opinion "TAUXXTAUXXTAUXX" is the expect greatest common divisor not "TAUXX" Below alignment shows that "TAUXXTAUXXTAUXX" is the largest common divisor:

"TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX"
"TAUXXTAUXXTAUXX"
                                    "TAUXXTAUXXTAUXX"
                                                                        "TAUXXTAUXXTAUXX"

Please correct me if I have an incorrect understanding of this problem.

Language Used for Code

JavaScript

Code used for Submit/Run operation

/**
 * Returns the greatest common divisor of the str1 and str2 parameters
 *
 * @param {string} str1
 * @param {string} str2
 * @return {string} which is the greatest common divisor of str1 and str2
 */
var gcdOfStrings = function(str1, str2) {
    // Determine big str and small str 
    // in the context of str1 and str2
    let bigStr;
    let smallStr;
    if (str1.length > str2.length) {
        bigStr = str1;
        smallStr = str2;
    } else {
        bigStr = str2;
        smallStr = str1;
    }
    // assume largest match and work back
    let ssi = smallStr.length; // small string index
    let bsi = bigStr.length; // big string index
    let ml = smallStr.length; // current length of match
    trySmallerMatch: for (; ml > 0; ml--) {
        let matchStr = smallStr.slice(0,ml)
        for (let j = 0;j < bigStr.length; j += ml) {
            // console.log(bigStr.slice(j,j+ml), matchStr, bigStr.slice(j,j+ml) != matchStr, j, bigStr.length);
            if (bigStr.slice(j,j+ml) != matchStr) {
                continue trySmallerMatch;
            }
        }
        // match found or all possibilities have been exhausted
        break;
    }
    return smallStr.slice(0,ml);
};

Expected behavior

I think this test case is incorrect: str1 = "TAUXXTAUXXTAUXXTAUXXTAUXX" str2 = "TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX" Expected = "TAUXX" In my opinion "TAUXXTAUXXTAUXX" is the expect greatest common divisor not "TAUXX" Below alignment shows that "TAUXXTAUXXTAUXX" is the largest common divisor:

"TAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXXTAUXX"
"TAUXXTAUXXTAUXX"
                                    "TAUXXTAUXXTAUXX"
                                                                        "TAUXXTAUXXTAUXX"

Please correct me if I have an incorrect understanding of this problem.

Screenshots

No response

Additional context

No response

exalate-issue-sync[bot] commented 1 month ago

Winston Tang commented: Hello shaw_fire,

Thank you for reaching out! Based on the problem statement, the greatest common divisor of the strings is defined as the longest string which, when repetitively concatenated, can yield (or "divide") the entirety of both original strings.

While "TAUXXTAUXXTAUXX" does repeat within the larger string, it doesn't cover the entirety of the shorter string ("TAUXXTAUXXTAUXXTAUXXTAUXX"). However, "TAUXX", does, making it the correct answer according to the identified constraints of this problem.

Please reread the constraints of the problem, and carefully check the definition and requirements of the greatest common divisor of the strings in the context of the problem. If you need further assistance, you can explore tutorials or discuss the issue on the LeetCode discussion forums.

Best Regards, LeetCode Support Team