new exercise

balanced parenthesis exercise
This commit is contained in:
MenglyS 2023-05-04 19:13:36 +07:00 committed by GitHub
parent 3e530e3f61
commit 2acede34b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 0 deletions

View File

@ -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.

View File

@ -0,0 +1,8 @@
function balancedParenthesis(str){
//code here
}
//do not delete below this line
module.exports = balancedParenthesis;

View File

@ -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);
});
})

View File

@ -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;

View File

@ -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);
});
})