AlgoPrecision

Stacks And Queues

Stack Operations

Visualize LIFO behavior with push, pop, and peek operations before moving into parentheses parsing and monotonic stack patterns.

O(n) time / O(n) space
LIFO: last in, first outoperation: parseresult: pending
Top
empty stack
Base

Time

O(n)

Space

O(n)

Step Note

Scan each bracket and keep opening brackets on the stack.

Details
function isValid(text: string) {
  const stack = []
  const pairs = { ')': '(', ']': '[', '}': '{' }
  for (const char of text) {
    if ('([{'.includes(char)) stack.push(char)
    else if (stack.pop() !== pairs[char]) return false
  }
  return stack.length === 0
}