InferenceAI

Enter at least two characters to search.

Start free

Balanced Brackets

easyStack15 min10 XP

Return True if every (, [ and { in s is closed by the matching bracket in the correct order. s contains only bracket characters.

Examples

EXAMPLE WALKTHROUGHflat

A guided view of the published example, not a trace of your code.

INPUT["()[]{}"]
TASKbalanced_brackets()
EXPECTED OUTPUTtrue

Start with the input values shown in this example.

Example 1 of 3

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

Output: true

Explanation: flat

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

Output: true

Explanation: nested

Input: s = "(]"

Output: false

Explanation: mismatch

Hint 1

Push opening brackets on a stack; a closing bracket must match the top.

Topics

DSAStackPython