Stacks and Queues
Balanced Parentheses
Walk a string of bracket characters. Push every opening bracket. On a closing bracket, pop and verify it matches; mismatch or empty-stack pop means unbalanced. At end of string, stack must be empty.
Algorithm
Canonical input "({[]})" (balanced) finishes with the stack empty and
the result true.
Basic Implementation
basic.go
package main
import "fmt"
func matchOpen(close byte) byte {
switch close {
case ')':
return '('
case ']':
return '['
case '}':
return '{'
}
return 0
}
func main() {
text := "({[]})"
stack := make([]byte, 0, 64)
balanced := true
for i := 0; i < len(text); i++ {
ch := text[i]
if ch == '(' || ch == '[' || ch == '{' {
stack = append(stack, ch)
} else {
if len(stack) == 0 || stack[len(stack)-1] != matchOpen(ch) {
balanced = false
break
}
stack = stack[:len(stack)-1]
}
}
if len(stack) != 0 {
balanced = false
}
fmt.Println(balanced)
}
Complexity
- Time: O(n)
- Space: O(n) worst case
Implementation notes
- Go: a plain
[]byteslice withappend/ re-slice is the smallest honest stack shape; the standard library has no dedicated stack type, which keeps the lesson on the explicit push/pop pattern. - The
matchOpenhelper documents the closing -> opening map without leaking object identity into the replay. - The replay highlights the current character, shows the stack updating each frame, and surfaces the final balanced/unbalanced verdict.
stack push/pop
Use a plain `[]byte` slice as the stack; push by `append(stack, ch)`, pop by `stack = stack[:len(stack)-1]`.
matching map
A small `matchOpen(close)` helper returns the expected opener for each closing bracket — three explicit cases keep the lesson compact.