Quick Overview

A coding question asking whether a string of round, square and curly brackets is properly nested, with every opening bracket closed by the same type in the right order. It tests ordering logic and edge cases such as mismatched types, stray closing brackets and openers that are never closed.

Check Whether a String of Three Bracket Types Is Properly Nested

Company: ByteDance

Role: Site Reliability Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given a string `s` made only of the characters `(`, `)`, `[`, `]`, `{` and `}`, return whether its brackets are properly nested. ### Function Signature ```python def is_properly_nested(s: str) -> bool: ``` ### Rules - The string is properly nested when every opening bracket is closed by a closing bracket of the same type, and brackets are closed in the reverse order in which they were opened, so that no two pairs cross. - A closing bracket with no unclosed opening bracket before it makes the string invalid. - An opening bracket that is never closed makes the string invalid. - Return `True` if the string is properly nested and `False` otherwise. ### Constraints - `1 <= len(s) <= 10^4` - `s` contains only the six characters `(`, `)`, `[`, `]`, `{` and `}`. ### Examples **Example 1** ```text Input: s = "([]{})" Output: True ``` The square and curly pairs both sit inside the round pair, and each is closed by its own type. **Example 2** ```text Input: s = "([)]" Output: False ``` The round bracket is closed while the square bracket opened after it is still open, so the pairs cross. **Example 3** ```text Input: s = "((" Output: False ``` Neither opening bracket is ever closed.

Overview: A coding question asking whether a string of round, square and curly brackets is properly nested, with every opening bracket closed by the same type in the right order. It tests ordering logic and edge cases such as mismatched types, stray closing brackets and openers that are never closed.

Given a string `s` made only of the characters `(`, `)`, `[`, `]`, `{` and `}`, decide whether its brackets are properly nested. The string is properly nested when every opening bracket is closed by a closing bracket of the same type, and brackets are closed in the reverse order in which they were opened, so that no two pairs cross. In addition: - A closing bracket with no unclosed opening bracket before it makes the string invalid. - An opening bracket that is never closed makes the string invalid. Return `True` if `s` is properly nested and `False` otherwise (`true` / `false` in JavaScript, Java and C++). ### Constraints - `1 <= len(s) <= 10^4` - `s` contains only the six characters `(`, `)`, `[`, `]`, `{` and `}`. ### Example 1 ```text Input: s = "([]{})" Output: True ``` The square pair and the curly pair both sit inside the round pair, and each pair is closed by its own type. ### Example 2 ```text Input: s = "([)]" Output: False ``` The round bracket is closed while the square bracket opened after it is still open, so the pairs cross.

Constraints

  • 1 <= len(s) <= 10^4
  • s contains only the six characters '(', ')', '[', ']', '{' and '}'.

Examples

Input: ('(',)

Expected Output: False

Explanation: Minimum length: a lone opening bracket is never closed.

Input: (')',)

Expected Output: False

Explanation: Minimum length: a lone closing bracket has nothing to close.

Hints

  1. Matching counts of each bracket type are not enough on their own: compare "([)]" with "([])".
  2. When a closing bracket appears, decide which earlier opening bracket it is allowed to close under the reverse-order rule.
  3. Consider both failure modes in the rules: a closer with nothing left to close, and an opener still unclosed when the string ends.

Properly Nested Bracket String

easyByteDanceSite Reliability Engineer

Given a string s made only of the characters (, ), [, ], { and }, decide whether its brackets are properly nested.

The string is properly nested when every opening bracket is closed by a closing bracket of the same type, and brackets are closed in the reverse order in which they were opened, so that no two pairs cross. In addition:

  • A closing bracket with no unclosed opening bracket before it makes the string invalid.
  • An opening bracket that is never closed makes the string invalid.

Return True if s is properly nested and False otherwise (true / false in JavaScript, Java and C++).

Constraints

  • 1 <= len(s) <= 10^4
  • s contains only the six characters (, ), [, ], { and }.

Example 1

Input:  s = "([]{})"
Output: True

The square pair and the curly pair both sit inside the round pair, and each pair is closed by its own type.

Example 2

Input:  s = "([)]"
Output: False

The round bracket is closed while the square bracket opened after it is still open, so the pairs cross.

Examples

Example 1
Input
('(',)
Output
False
Notes
Minimum length: a lone opening bracket is never closed.
Example 2
Input
(')',)
Output
False
Notes
Minimum length: a lone closing bracket has nothing to close.

Constraints

  • •1 <= len(s) <= 10^4
  • •s contains only the six characters '(', ')', '[', ']', '{' and '}'.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def is_properly_nested(s) -> bool:
Sample call
is_properly_nested('(')
Language
Case 1
Input
('(',)
Expected
False
Case 2
Input
(')',)
Expected
False

Browse More Questions

Show the approach

Approach

Scan the string from left to right while keeping a stack of the opening brackets that are still unclosed, the most recent on top. An opening bracket is pushed. A closing bracket may only close the most recently opened bracket that is still open (closing in reverse order of opening is exactly what keeps pairs from crossing), so it is accepted only when the stack is non-empty and its top is the opener of the same type; the top is then popped. If the stack is empty, the closer has nothing to close; if the top is a different type, the closer either mismatches its opener or would close an earlier bracket while a later one is still open. Either way the answer is False immediately. Invariant: after every prefix processed without failure, the stack holds exactly the unclosed openers of that prefix in opening order, and every closed pair in the prefix is same-type and non-crossing. At the end the string is properly nested exactly when no opener remains unclosed, i.e. the stack is empty. Edge cases: a single character is never valid; any odd-length string fails; equal per-type counts are not sufficient ("([)]", ")("); a closer after a valid prefix with nothing open ("()[]}{") fails even though the counts balance.

Time complexity:
O(n)
Space complexity:
O(n)