R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

{ Bracket
Balancing }

The Question

One of your colleagues insists on writing all code in Notepad, resulting in code that won't run because the brackets, braces, and parenthesis are not properly balanced. You decide to write a bracket validator to check whether the brackets /braces/ parenthesis are valid.

Notes

  • You have already written a RegEx to get rid of the non-brackets from the input string (i.e. your input will be all brackets, parenthesis, and braces)
  • For the purpose of the problem these are considered 'brackets': 

[ ] ( ) { }

What you need to do:

Consider these to be valid pairs:

[  and   ] 

(   and   

{   and  } 

 

Write an efficient function that tells us whether or not an input string's brackets are properly nested.

 

"{ [ ] ( ) }" should return true

"{ [ ( ] ) }" should return false

"{ [ }" should return false

"{ [ ( [ { ( )[ ]{ } } ] ) ] }" should return true

Approach

  • You have "opening"- ( { [ - and closing - ) { ] - brackets
  • Each closing bracket should correspond to the nearest opening bracket that it pairs with
  • Every opening and closing bracket should be in a pair

Approach, continued

{ [  ( ( )  )] }

Approach, continued

{ [  ( ( )  )] }

{

Approach, continued

{ [  ( ( )  )] }

{

{

[

Approach, continued

{ [  ( ( )  )] }

{

{

[

(

Approach, continued

{ [  ( ( )  )] }

{

{

[

(

(

Approach, continued

{ [  ( ( )  )] }

{

{

[

(

(

)

Approach, continued

{ [  ( ( )  )] }

The top bracket closes the one below it so remove both

{

[

(

(

)

Approach, continued

{ [  ( ( )  )] }

{

(

[

Approach, continued

{ [  ( ( )  ] }

{

(

)

The top bracket closes the one below it so remove both

[

Approach, continued

{ [  ( ( )  )] }

{

[

Approach, continued

{ [  ( ( )  ) ] }

{

{

[

]

The top bracket closes the one below it so remove both

Approach, continued

{ [  ( ( )  ) ] }

{

Approach, continued

{ [  ( ( )  ) ] }

{

{

The top bracket closes the one below it so remove both

}

Approach, continued

{ [  ( ( )  ) ] }

We have checked every bracket in the input and since we have no unmatching brackets, we know that the brackets were balanced

What data structure did this approach remind you of ?

What data structure did this approach remind you of ?

a STACK!

Possible Solution

function hasBalancedBrackets(inputString) {
	var inputBrackets = inputString.match(/[[\](){}]/g);
	var bracketPairs = {  
		'[' : ']',
		'(' : ')',
		'{' : '}'
	}; 
	var brackets = [ ];
	if (!inputString.length || !inputBrackets.length) 
	        return true;
	inputBrackets.forEach (function (bracket) {
		var lastBracket = brackets[brackets.length - 1];
		if (bracketPairs[lastBracket] === bracket) 
			brackets.pop(); 
		else 
			brackets.push(bracket);
	});

	return brackets.length === 0; 
}

Possible Solution

function hasBalancedBrackets(inputString){
      var braces = inputString.match(/[[\](){}]/g);
      var e;

      while ((e = /\(\)/.exec(braces)) || 
            (e = /\[\]/.exec(braces)) || 
            (e = /\{\}/.exec(braces))) 
      {
            braces = braces.replace(e[0], '');
      }

      return !braces;
}

Conclusion  

REPL.IT solutions

 

- Data structure principles can come in handy even when you are not explicitly creating a certain structure

-You can use arrays and array methods in Javascript to create an array that basically behaves like a stack/queue

Copy of Copy of Bracket Balancing

By Tom Kelly

Copy of Copy of Bracket Balancing

Technical interview problem for determining whether the brackets/parenthesis in a string are balanced

  • 1,329