diff --git a/13_balancedParenthesis/README.md b/13_balancedParenthesis/README.md new file mode 100644 index 0000000..46eee00 --- /dev/null +++ b/13_balancedParenthesis/README.md @@ -0,0 +1,21 @@ +# Exercise 13 - Balanced Parenthesis + +Write a JavaScript function that takes a string as input and returns true if it contains a balanced set of parentheses and false otherwise. The function should, for example, return true for the following strings: + +- "()" +- "{()}" +- "()[]" + +and return false for following string: + +- "(()" +- "{}}}" +- "[(])}" + +## Hint: + +To solve this problem, you can use a stack data structure to keep track of when parenthesis open and close. + +- Each opening parenthesis in the string is pushed onto the stack by looping through it. +- If you encounter a closing parenthesis, take the top element from the stack and see if it matches. Continue iterating over the string if they match. Return false if they do not match. +- If the stack is empty at the end of the loop, return true; otherwise, return false. \ No newline at end of file diff --git a/13_balancedParenthesis/balancedParenthesis.js b/13_balancedParenthesis/balancedParenthesis.js new file mode 100644 index 0000000..27f2ce0 --- /dev/null +++ b/13_balancedParenthesis/balancedParenthesis.js @@ -0,0 +1,8 @@ +function balancedParenthesis(str){ + //code here + +} + + +//do not delete below this line +module.exports = balancedParenthesis; \ No newline at end of file diff --git a/13_balancedParenthesis/balancedParenthesis.spec.js b/13_balancedParenthesis/balancedParenthesis.spec.js new file mode 100644 index 0000000..87ac17e --- /dev/null +++ b/13_balancedParenthesis/balancedParenthesis.spec.js @@ -0,0 +1,33 @@ +const balancedParenthesis = require('./balancedParenthesis'); + +describe("balanced parenthesis", () => { + + test("(()) is return true", () => { + expect(balancedParenthesis("(())")).toBe(true); + }); + test.skip("empty string is true", () => { + expect(balancedParenthesis("")).toBe(true); + }); + test.skip("()) is return false", () => { + expect(balancedParenthesis("())")).toBe(false); + }); + test.skip("{[()]} is return true", () => { + expect(balancedParenthesis("{[()]}")).toBe(true); + }); + test.skip("{[()]}( is return false", () => { + expect(balancedParenthesis("{[()]}(")).toBe(false); + }); + test.skip("((((((( is return false", () => { + expect(balancedParenthesis("(((((((")).toBe(false); + }); + test.skip("()[]{} is return true", () => { + expect(balancedParenthesis("()[]{}")).toBe(true); + }); + test.skip("([{}]) is return true", () => { + expect(balancedParenthesis("([{}])")).toBe(true); + }); + test.skip("{{{{}}}}()() is return true", () => { + expect(balancedParenthesis("{{{{}}}}()()")).toBe(true); + }); + +}) \ No newline at end of file diff --git a/13_balancedParenthesis/solution/balancedParenthesis-solution.js b/13_balancedParenthesis/solution/balancedParenthesis-solution.js new file mode 100644 index 0000000..9c53365 --- /dev/null +++ b/13_balancedParenthesis/solution/balancedParenthesis-solution.js @@ -0,0 +1,32 @@ +function balancedParenthesis(str){ + const stack = []; + const openP = { + "(" : ")", + "[" : "]", + "{" : "}" + }; + const closeP = { + ")" : true, + "]" : true, + "}" : true + }; + + for (let i = 0; i < str.length; i++){ + const char = str[i]; + + if (openP[char]){ + stack.push(char); + } + else if (closeP[char]){ + const pop = stack.pop(); + + if (!pop || openP[pop] != char){ + return false; + } + } + } + + return stack.length == 0 ? true : false; +} + +module.exports = balancedParenthesis; \ No newline at end of file diff --git a/13_balancedParenthesis/solution/balancedParenthesis-solution.spec.js b/13_balancedParenthesis/solution/balancedParenthesis-solution.spec.js new file mode 100644 index 0000000..14426c2 --- /dev/null +++ b/13_balancedParenthesis/solution/balancedParenthesis-solution.spec.js @@ -0,0 +1,33 @@ +const balancedParenthesis = require('./balancedParenthesis-solution'); + +describe("balanced parenthesis", () => { + + test("(()) is return true", () => { + expect(balancedParenthesis("(())")).toBe(true); + }); + test("empty string is true", () => { + expect(balancedParenthesis("")).toBe(true); + }); + test("()) is return false", () => { + expect(balancedParenthesis("())")).toBe(false); + }); + test("{[()]} is return true", () => { + expect(balancedParenthesis("{[()]}")).toBe(true); + }); + test("{[()]}( is return false", () => { + expect(balancedParenthesis("{[()]}(")).toBe(false); + }); + test("((((((( is return false", () => { + expect(balancedParenthesis("(((((((")).toBe(false); + }); + test("()[]{} is return true", () => { + expect(balancedParenthesis("()[]{}")).toBe(true); + }); + test("([{}]) is return true", () => { + expect(balancedParenthesis("([{}])")).toBe(true); + }); + test("{{{{}}}}()() is return true", () => { + expect(balancedParenthesis("{{{{}}}}()()")).toBe(true); + }); + +}) \ No newline at end of file