Skip to content

modified by yazan eleyan #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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