CodePark/ problems / Valid Brackets
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Stacks

Valid Brackets

easyabout 12 minutes in an interview

s contains only the six bracket characters ( ) [ ] { }. Return true if every bracket has a proper partner, and false if not.

Think of it like this. Read s from left to right:

  • A closing bracket must be the same kind as the most recent bracket that is still open. "([])" works, but "([)]" doesn't.
  • A closing bracket with nothing open before it is a mistake.
  • When you reach the end, nothing may be left open.

An empty string gives true. s can be up to 100,000 characters long.

Input: s = "[(){}]" Output: true
Input: s = "([)]" Output: falseThe ) arrives while [ is the most recent open bracket, so they don't match.
Input: s = "((" Output: falseTwo brackets are never closed.