Posts

Showing posts from July, 2019

Bracket Validator

You're working with an intern that keeps coming to you with JavaScript code that won't run because the braces, brackets, and parentheses are off. To save you both some time, you decide to write a braces/brackets/parentheses validator. Let's say: '(', '{', '[' are called " openers ." ')', '}', ']' are called " closers ." Write an efficient function that tells us whether or not an input string's openers and closers are properly nested. Examples: "{ [ ] ( ) }" should return True "{ [ ( ] ) }" should return False "{ [ }" should return False Simply making sure each opener has a corresponding closer is not enough —we must also confirm that they are correctly ordered . For example, "{ [ ( ] ) }" should return False , even though each opener can be matched to a closer. We can do this in O ( n ) O(n) O ( n ) time and space