Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function isValid(s) {
const stack = [];

for (let char of s) {
switch (char) {
case '(': case '[': case '{':
// If the character is an opening bracket, push it onto the stack.
stack.push(char);
break;
case ')':
if (stack.pop() !== '(') {
return false; // Mismatched closing bracket.
}
break;
case ']':
if (stack.pop() !== '[') {
return false; // Mismatched closing bracket.
}
break;
case '}':
if (stack.pop() !== '{') {
return false; // Mismatched closing bracket.
}
break;
default:
// If the character is not a bracket, ignore it.
}
}

// After processing all characters, the stack should be empty if the string is valid.
return stack.length === 0;
}

console.log(isValid("()")); // true
console.log(isValid("()[]{}")); // true
console.log(isValid("(]")); // false
1 change: 1 addition & 0 deletions text.text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is modified by Yazan Eleyan