LeetCode-Feedback / LeetCode-Feedback

702 stars 342 forks source link

Missing Test Case - 1790. Check if One String Swap Can Make Strings Equal #26875

Closed RahZero0 closed 4 days ago

RahZero0 commented 1 week ago

LeetCode Username

WwRXCklvRa

Problem Number, Title, and Link

  1. Check if One String Swap Can Make Strings Equal https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/

Bug Category

Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)

Bug Description

Bug Type: Logical error leading to incorrect swap detection.

Bug Summary: The function incorrectly tracks mismatched indices when the first character of s1 and s2 differ. This happens because index1 and index2 are initialized to 0, which is a valid index in the string. As a result, the first mismatch at index 0 is not properly recorded, causing incorrect swap validation.

Issue Details: Incorrect Index Tracking:

index1 and index2 are initialized to 0. If the first character itself is a mismatch (s1[0] != s2[0]), index1 remains 0, causing the actual first mismatch to be ignored. When a second mismatch occurs, index2 is also assigned 0, overwriting the actual second mismatch index. False Positive Swap Detection:

Due to the incorrect index tracking, the function might mistakenly believe a valid swap exists even when multiple swaps are required. This results in the function returning true when it should return false.

Language Used for Code

Java

Code used for Submit/Run operation

class Solution {
    public boolean areAlmostEqual(String s1, String s2) {
        int index1 = 0, index2 = 0;
        char[] charArray1 = s1.toCharArray();
        char[] charArray2 = s2.toCharArray();
        for(int i = 0; i < charArray1.length; i++) {
            if(charArray1[i] != charArray2[i]) {
                if(index1 ==  0) 
                    index1 = i;
                else if(index2 == 0)
                    index2 = i;
                else
                    return false;
            }
        }
        return charArray1[index2] == charArray2[index1] && charArray1[index1] == charArray2[index2];
    }
}

Expected behavior

The function areAlmostEqual(s1, s2) should return true only if s2 can be obtained from s1 by swapping exactly one pair of characters. Otherwise, it should return false.

Expected Conditions for true:

  1. s1 and s2 must have exactly two mismatched characters.
  2. Swapping those two mismatched characters in s1 must make it equal to s2.
  3. If s1 and s2 are already identical, return true (since no swap is needed).

Expected Conditions for false:

  1. If s1 and s2 have more than two mismatched characters.
  2. If swapping the mismatched characters does not make s1 equal to s2.
  3. If s1 and s2 have different lengths.

Test Cases & Expected Output:

Test Case Input (s1, s2) Expected Output Explanation
✅ Case 1: No swap needed "abc", "abc" true The strings are already equal, no swap required.
✅ Case 2: One valid swap "bank", "bkan" true Swapping 'a' and 'k' makes the strings equal.
❌ Case 3: More than one swap needed "bankb", "kanbk" false Requires two swaps ('b' ↔ 'k' at index 0 and 'k' ↔ 'b' at index 3 & 4), so it's invalid.
❌ Case 4: Three mismatched characters "appla", "lppal" false More than two mismatches, cannot be fixed with one swap.

Behavior of Function for s1 = "bankb", s2 = "kanbk"

Input:

s1: "bankb"

s2: "kanbk"

Step-by-Step Execution:

Index s1 Character s2 Character Mismatch?
0 'b' 'k' ✅ (Mismatch ~1)
1 'a' 'a' ❌ (No mismatch)
2 'n' 'n' ❌ (No mismatch)
3 'k' 'b' ✅ (Mismatch ~2)
4 'b' 'k' ✅ (Mismatch ~3)

Mismatch Condition Check:

More than two mismatches found at indices 0, 3, and 4.

The function should return false because a single swap cannot fix the issue.

Expected Output:

false

Behavior of Function for s1 = "appla", s2 = "lppal"

Input:

s1: "appla"

s2: "lppal"

Step-by-Step Execution:

Index s1 Character s2 Character Mismatch?
0 'a' 'l' ✅ (Mismatch ~1)
1 'p' 'p' ❌ (No mismatch)
2 'p' 'p' ❌ (No mismatch)
3 'l' 'a' ✅ (Mismatch ~2)
4 'a' 'l' ✅ (Mismatch ~3)

Mismatch Condition Check:

More than two mismatches found at indices 0, 3, and 4.

The function should return false because a single swap cannot fix the issue.

Expected Output:

false

Why This Test Cases are Important:

Screenshots

Image

Image

Image

Additional context

No response

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

LeetCode Support commented: Hello,

Your reported issue has been relayed to our team for thorough investigation. We appreciate your patience as we work to address and resolve this matter. We will reach out to you when we have updates regarding the issue.

If you have any further questions or concerns in the meantime, please feel free to let us know.

Best regards, LeetCode Support Team

exalate-issue-sync[bot] commented 4 days ago

Epiphania_Ekenimoh commented: Thank you for your time.

Your feedback has been used to enhance the problem. As a token of our appreciation, your LeetCode account has been credited with 100 LeetCoins.

If you have any more questions or additional feedback, please don't hesitate to let us know. Your continued support is invaluable to us!

Best regards, The LeetCode Team