LeetCode-Feedback / LeetCode-Feedback

670 stars 330 forks source link

Incorrect Test Case. 20. Valid Parentheses #25067

Open andrewsu31098 opened 2 hours ago

andrewsu31098 commented 2 hours ago

LeetCode Username

avsu

Problem Number, Title, and Link

  1. Valid Parentheses https://leetcode.com/problems/valid-parentheses/

Bug Category

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

Bug Description

s = "(])" This testcase fails. I asked on StackOverflow and they told me I should submit a report. Returns true on leetcode.

Language Used for Code

C++

Code used for Submit/Run operation

#include <unordered_map>
#include <stack>
class Solution {
public:
    bool isValid(string s) {
        std::stack<char> myStack;
        for (int i = 0; i<s.size(); i++){
            if ((s[i] == '(') || (s[i] == '{') || (s[i] == '[')){
                myStack.push(s[i]);
            }
            else {
                if (myStack.empty()){
                    return false;
                }
                if ((s[i]==')') && (myStack.top()=='(')){
                    myStack.pop();
                }
                else if ((s[i]==']') && (myStack.top()=='[')){
                    myStack.pop();
                }
                else if ((s[i]=='}') && (myStack.top()=='{')){
                    myStack.pop();
                }
                // else { return false; }
                // If I don't include this else statement the code fails.
            }
        }
        return myStack.empty();
    };
};

Expected behavior

Should return false. It correctly returns false in different C++ compilers.

Screenshots

result

Additional context

No response

exalate-issue-sync[bot] commented 2 hours ago

LeetCode Support commented: Thank you for reaching out and providing details about your concern with the "Valid Parentheses" problem. Upon reviewing your code, it seems there is a missing else statement that would return false when mismatched parentheses are encountered. The LeetCode platform and problem statement correctly specify that the input "(])" should return false because the parentheses aren't matched correctly. I recommend revisiting your code to include logic that handles mismatches. If you have any other questions or need assistance, feel free to consult the community forums or LeetCode's editorial for further guidance. We appreciate your engagement and understanding.

LeetCode Support Team

andrewsu31098 commented 2 hours ago

The code should correctly work even without the else statement.