Skip to content

Latest commit

 

History

History
43 lines (28 loc) · 2.03 KB

File metadata and controls

43 lines (28 loc) · 2.03 KB

Stack + Queue Brackets

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

Approach & Efficiency

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.

Solution

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/

Test code in main module: collected 9 items stack_queue_brackets/stack_queue_brackets.py .....  100%, 9 passed in 0.01s

Code Links

Stack Queue Brackets

Tests of Stack Queue Brackets