Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

balanced-parenthesis #31

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

Merged
ashokdey merged 1 commit into knaxus:master from SumeetHaryani:master
Oct 4, 2019
Merged
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
2 changes: 2 additions & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack)
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
- [Minimum Stack](src/_DataStructures_/Stack/min-stack)
- [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis)

- [Queue](src/_DataStructures_/Queue)
- [Weave](src/_DataStructures_/Queue/weave)

Expand Down
43 changes: 43 additions & 0 deletions src/_DataStructures_/Stack/balanced-parenthesis/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
Given an expression string exp , write a program to examine whether the pairs and
the orders of "{","}","(",")","[","]" are correct in expression.

Example:
Input: exp = "[()]{}{[()()]()}"
Output: true

Input: exp = "[(])"
Output: false
*/


const Stack = require('../index');

function checkBalancedParenthesis(expression) {
let s = new Stack();
for (let i = 0; i < expression.length; i++) {
const char = expression[i];
if (char === '{' || char === '(' || char === '[') {
//If current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to stack
s.push(char);
} else {
if (s.isEmpty()) {
//when we have only right parenthesis or brackets in expresion
return false;
} else if (char === '}' && s.peek() !== '{' || char === ')' && s.peek() !== '(' || char === ']' && s.peek() !== '[') {
return false;
}
//If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then pop it from stack
s.pop();
}
}
if (s.isEmpty()) {
//expression has balanced parenthesis
return true;
} else {
return false;
}
}

console.log(checkBalancedParenthesis("{()}[]")); //true
console.log(checkBalancedParenthesis("{()}}")); //false
3 changes: 3 additions & 0 deletions src/_DataStructures_/Stack/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Stack {
peek() {
return this.data[this.data.length - 1];
}
isEmpty(){ //check if stack is empty
return this.data.length==0;
}
}

module.exports = Stack;

AltStyle によって変換されたページ (->オリジナル) /