Skip to content

Balanced parentheses problem added #167

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
- [Maximum Product of Three Numbers](src/_Problems_/max-product-of-3-numbers)
- [Next Greater for Every Element in an Array](src/_Problems_/next-greater-element)
- [Compose Largest Number](src/_Problems_/compose-largest-number)
- [Balanced Parentheses](src/_Problems_/balanced-parentheses)

### Searching

Expand Down
17 changes: 0 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/_Problems_/balanced-parentheses/balanced-parentheses.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { balancedParantheses } = require('.');

describe('Balanced parantheses in string', () => {
let str1, str2, str3, str4;

beforeEach(() => {
str1 = '(())';
str2 = '(((';
str3 = ')(';
str4 = '((()';
});

it('Should return TRUE for `(())`', () => {
expect(balancedParantheses(str1)).toBe(true);
});

it('Should return FALSE for `(((`', () => {
expect(balancedParantheses(str2)).toBe(false);
});

it('Should return TRUE for `)(`', () => {
expect(balancedParantheses(str3)).toBe(false);
});

it('Should return TRUE for `((()`', () => {
expect(balancedParantheses(str4)).toBe(false);
});
});
27 changes: 27 additions & 0 deletions src/_Problems_/balanced-parentheses/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Given a string containing characters and "(" , ")" determine if input string is valid if:
- Open parantheses is closed by same number & type of parantheses.
Note that an empty string is also considered valid and any alphanumeric char can be inserted in between pratheses.

Example :
((())) is balanced.
((() is not balanced.
)( is not balanced.
We will solve this problem using ES6 - Array Method reduce().
*/

function balancedParantheses(string) {
return !string.split('').reduce(function(prev, char) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about an arrow function here?

if (prev < 0) return prev;

if (char === '(') return ++prev;

if (char === ')') return --prev;

return prev;
Comment on lines +15 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a switch case here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think switch case isn't needed here since we have to check two variables char & prev here. can i use ternary operator instead ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this

  return !string.split('').reduce((prev, char) => {
    const result = prev < 0 ? prev : char === '(' ? ++prev : char === ')' ? --prev : prev;
    return result;
  }, 0);

Copy link
Member

@ashokdey ashokdey Jul 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above snippet is not readable enough

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, if else pattern will be fine right ? switch case will add more code since we have to check cases for prev & char.

}, 0);
}

module.exports = {
balancedParantheses,
};