Tag balance: hints, no solution

Easy · Stack

A template editor stores a line of markup as a list of tag events. An opening event is written as a plus sign followed by the tag name, and a closing event as a minus sign followed by the tag name, so the list may read plus div, plus b, minus b, minus div.

The line is balanced when every opening event is closed by a later event with the same name, and the pairs nest properly: the most recently opened tag must be the next one closed. Tag names are case-sensitive and compare exactly.

Return true if the line is balanced and false otherwise. An empty list is balanced.

Examples

Input
tags = ["+div", "+b", "-b", "-div"]
Output
true
Note
Every tag is closed in the reverse order it was opened.
Input
tags = ["+ul", "+li", "-ul", "-li"]
Output
false
Note
The closing ul arrives while li is still open, so nesting is broken.

Constraints

  • 0 <= tags.length <= 120000
  • Each event is + or - followed by 1 to 10 lowercase letters or digits
  • Names compare exactly, so item and items differ

Hint 1: a nudge

Look at each closing event on its own. Which earlier opening event must it be paired with for the line to be valid?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first