All problems

Layered Settings

Medium · stack

A theme editor keeps named numeric settings in layers. There is always a base layer, and begin opens a new empty layer on top of the others. You are given a list of commands to run in order. Each command is one of:

name is 1 to 8 lower-case letters and digits, and value is a whole number from 0 to 10^9. Return a list with the answer to every "get" command, in the order they appear.

Examples

Example 1

Input: commands = ["set a 5", "begin", "set a 7", "get a", "end", "get a", "get b"]

Output: [7, 5, -1]

Inside the new layer a is 7, hiding the 5. After end the 5 is visible again, and b was never set.

Example 2

Input: commands = ["begin", "set x 1", "begin", "get x", "set y 2", "end", "get y", "end", "get x"]

Output: [1, -1, -1]

x is visible through both layers. y disappears when its layer ends, and x disappears when the outer layer ends.

Example 3

Input: commands = ["set a 1", "set a 2", "get a"]

Output: [2]

Setting a name twice in the same layer replaces the earlier value.

Constraints