-
Notifications
You must be signed in to change notification settings - Fork 268
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
}); | ||
}); |
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) { | ||
if (prev < 0) return prev; | ||
|
||
if (char === '(') return ++prev; | ||
|
||
if (char === ')') return --prev; | ||
|
||
return prev; | ||
Comment on lines
+15
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use a switch case here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above snippet is not readable enough There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}; |
There was a problem hiding this comment.
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?