Balanced Brackets
Problem
Given a string of brackets, where there are 3 types of brackets: round brackets, "(" and ")", square brackets "[" and "]", and squiggly brackets "{" and "}", write a function that takes in a string of brackets and returns a boolean representing whether or not that string is balanced
A string is said to be balanced if there are as many opening brackets as it has closing brackets, and every opening bracket is correctly matched by an appropriate closing bracket
Code
Python
JavaScript
TypeScript
Java
C++
Space-Time Complexity
Time | Space | |
---|---|---|
Worse | O(n) | O(n) |
Where n is the length of the input array
Notes
keep in mind
We're going to be using a stack because we want to keep track of every pair of matching brackets. For every opening bracket, we want to find its corresponding matching closing bracket. More precisely, we want to keep track of the last opening bracket that we've seen
solution 1
Initialize openingBrackets
, which is just going to be a string of all the different opening brackets, it could also be an array, and it's basically going to help us check if whatever character we're at when we traverse our string, is an opening bracket. This variable will allow us to ask, is that character included in the string or in this array?
Initialize closingBrackets
, which is similar to openingBrackets
Initialize matchingBrackets
, which is going to be a Hash Table, or a Python dictionary, or a JavaScript object, that's going to map every closing bracket to its corresponding opening bracket. This is going to allow us to have very clean code so that when we traverse our string, run into a closing bracket, and check if the last value in the stack is the matching opening bracket, we can just check it from the Hash Table
Initialize stack
to be an empty array
Start traversing our string of brackets. At each character in the string, we'll check if our character is an opening bracket. If it is, then we'll store the character in our stack. If not, then we'll first check if our stack is empty, because if our stack is empty then that means we don't have an opening bracket that we could try to match our current closing bracket to i.e. we know for certain the closing bracket that we're looking at is alone and unmatched and thus our string must be imbalanced, and we must return False
.
If we do have brackets in our stack, then we'll want to check what the last bracket was. We want to check that the last bracket actually corresponds to the type of the current bracket that we're at, because if the last bracket doesn't correspond to the type of our current closing bracket, then we're also in a situation where our string is imbalanced, and we must return False
. Otherwise, if the last value in the stack does match the type of our current bracket, then we can pop off the last value off of the stack.
Once we're done iterating through the string, before we return True
and say that our string is indeed balanced, we need to check that our stack is empty, because if our stack isn't empty then that means we have an opening bracket somewhere in the string that is unmatched