From f723a5dc847ce055652a496367d7071066b08212 Mon Sep 17 00:00:00 2001 From: Yazan Eleyan Date: Sun, 5 Nov 2023 17:35:03 +0200 Subject: [PATCH] modified by yazan eleyan --- demo.js | 36 ++++++++++++++++++++++++++++++++++++ text.text | 1 + 2 files changed, 37 insertions(+) create mode 100644 demo.js create mode 100644 text.text diff --git a/demo.js b/demo.js new file mode 100644 index 0000000..2a3bc39 --- /dev/null +++ b/demo.js @@ -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 diff --git a/text.text b/text.text new file mode 100644 index 0000000..d444bcd --- /dev/null +++ b/text.text @@ -0,0 +1 @@ +this is modified by Yazan Eleyan