Beyond Big O

Balanced delimiters

Balanced delimiters

Easy

Your task

  • Return true when every opening delimiter has a matching closing delimiter of the same kind, in the correct nesting order.
  • Otherwise return false.
  • Supported pairs are (), [] and {}.

Required API

  • BalancedDelimiters
    func BalancedDelimiters(text string) bool

Behavior

  • Empty text is balanced.
  • Adjacent balanced groups are valid, but crossed pairs such as ([)] are not.

Constraints

  • The input is valid: text has 0–1,000 characters and contains only these six ASCII delimiter characters.
  • Other characters are outside the input contract.
  • Separate calls must not share state.
  • Aim for O(n) time and O(n) extra space using only the standard library.
  • Explain what an unmatched opening delimiter means on the stack.
Notes and example notation

Examples use {"text": "..."}.

Examples

The runner calls your functions directly; you do not need to parse JSON.

Example 1 · empty

Arguments
text
""
Expected return
true
Test data
Input
{"text":""}
Expected output
true

Example 2 · nested

Arguments
text
"{[()]}"
Expected return
true
Test data
Input
{"text":"{[()]}"}
Expected output
true

Example 3 · crossed

Arguments
text
"([)]"
Expected return
false
Test data
Input
{"text":"([)]"}
Expected output
false

Example 4 · adjacent

Arguments
text
"()[]{}"
Expected return
true
Test data
Input
{"text":"()[]{}"}
Expected output
true
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…