Skip to content

Instantly share code, notes, and snippets.

@remainstheday
Created October 23, 2019 21:38
Show Gist options
  • Save remainstheday/8c0ffe51f7857b823e7cd9e76811cfd4 to your computer and use it in GitHub Desktop.
Save remainstheday/8c0ffe51f7857b823e7cd9e76811cfd4 to your computer and use it in GitHub Desktop.
/*
# A bracket is considered to be any one of the following characters: ( ) { } [ or ].
# Two brackets are a matched pair if the an opening bracket i.e. ( [ or { occurs to the left of a closing bracket i.e. ) ] or } of the same type. There are three types of matched pairs of brackets: [], {}, and ().
#
# A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For example, {[(])} is not balanced because the contents in between { and } are not balanced. The pair of square brackets encloses a single, unbalanced opening bracket, (, and the pair of parentheses encloses a single, unbalanced closing square bracket, ].
#
# By this logic, we say a sequence of brackets is balanced if the following conditions are met:
#
# It contains no unmatched brackets.
# The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets.
# Given strings of brackets, determine whether each sequence of brackets is balanced. If a string is balanced, return YES. Otherwise, return NO.
#
# Function Description - Complete the function isBalanced in the editor below.
#
# isBalanced has the following parameter(s):
# s: a string of brackets
#
# Constraints
# All characters are in the sequences { } ( ) and [ ].
#
# Output Format
# For each string, return YES or NO: YES if the sequence is balanced or NO if it is not.
# Sample Input:
# 3
# ()(){}
# (({[[]]))))
# {[[{()}]]}
#
# Sample Output:
# YES
# NO
# YES
*/
/* Check if the provided arguements are complementary brackets */
function checkPairs(bracket1, bracket2) {
const brackets = {
openParen: '(',
closeParen: ')',
openCurly: '{',
closeCurly: '}',
openSquare: '[',
closeSquare: ']'
};
if (bracket1 === brackets.openParen) {
if (bracket2 !== brackets.closeParen) {
return false;
}
}
if (bracket1 === brackets.openCurly) {
if (bracket2 !== brackets.closeCurly) {
return false;
}
}
if (bracket1 === brackets.openSquare) {
if (bracket2 !== brackets.closeSquare) {
return false;
}
} else {
return true;
}
}
function isBalanced(brackets) {
const bracketsArr = Array.from(brackets);
if (bracketsArr.length % 2 !== 0) return console.log('NO'); // an odd number of brackets is always false.
/* test for bracket pairs: ()(){} */
if (checkPairs(bracketsArr[0], bracketsArr[1])) {
for (let i = 0; i < bracketsArr.length; i++) {
if (!checkPairs(bracketsArr[i], bracketsArr[i + 1]))
return console.log('NO');
}
} else {
/* test for nested brackets: {[[{()}]]} */
for (let i = 0; i < bracketsArr.length; i++) {
const firstEl = bracketsArr.shift();
const lastEl = bracketsArr.pop();
if (!checkPairs(firstEl, lastEl)) return console.log('NO');
}
}
return console.log('YES');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment