Open
Description
Bug Report for https://neetcode.io/problems/validate-parentheses
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
There is no testcase for a possible invalid parentheses. If there is an additional closing bracket, one's code should fail. I created the following solution that passed all test cases and was accepted despite it being an invalid solution when I created my own test case. Here is my solution that passed all test cases yet failed a testcase with "()]" by returning true: class Solution {
public:
bool isValid(string s) {
if (s[0] == ')' || s[0] == '}' || s[0] == ']') {
return false;;
}
for (int i = 0; i < s.size(); i++) {
if (s[i] == ')' || s[i] == '}' || s[i] == ']') {
continue;
}
int idxToReach = i + 1;
int index = i + 1;
if (s[i] == '(') {
while (idxToReach < s.size()) {
if (s[index] == ')' && index == idxToReach) {
break;
} else if (s[index] == '(' || s[index] == '{' || s[index] == '[') {
idxToReach += 2;
} else if (s[index] != ')' && index == idxToReach) {
return false;
}
index++;
}
} else if (s[i] == '[') {
while (idxToReach < s.size()) {
if (s[index] == ']' && index == idxToReach) {
break;
} else if (s[index] == '(' || s[index] == '{' || s[index] == '[') {
idxToReach += 2;
} else if (s[index] != ']' && index == idxToReach) {
return false;
}
index++;
}
} else if (s[i] == '{') {
while (idxToReach < s.size()) {
if (s[index] == '}' && index == idxToReach) {
break;
} else if (s[index] == '(' || s[index] == '{' || s[index] == '[') {
idxToReach += 2;
} else if (s[index] != '}' && index == idxToReach) {
return false;
}
index++;
}
}
if (idxToReach >= s.size()) {
return false;
}
}
return true;
}
};
Metadata
Metadata
Assignees
Labels
No labels