Write a function called validate brackets Arguments: string Return: boolean
- representing whether or not the brackets in the string are balanced
There are 3 types of brackets:
-
Round Brackets : ()
-
Square Brackets : []
-
Curly Brackets : {}
-
This will use code from Stack
I enumerated the string characters so that the node being pushed to a stack would contain the data and the index of the character. Essentially, the logic is to push to the stack for an opening bracket and then pop for a closing bracket. Then, I used conditional logic to make sure that the index of the closing bracket was not before the opening bracket, the stack is not empty when trying to pop from it upon coming across a closing bracket, and making sure that the brackets match the appropriate bracket type.
Time: O(1)
- because only deals with one node at a time for all of these methods, since the nodes being accessed are only at the top of the stack.
- However, this method overall may use also 0(n^2), because I am calling on two iterative methods of a dictionary to access the keys and values.
Space: O(n)
- For the space of the character string list and the space of the Stack individually, O(2n) for both of those combined in memory.
Terminal command for general main module: python3 -m stack_queue_brackets.stack_queue_brackets
Terminal command for testing in pytest: python3 -m pytest stack_queue_brackets/
