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