Problem · Hash Table
Set Command Simulation
Learn this problemProblem statement
Simulate a set of strings over a finite batch of commands in operations.
["PUT", value]insertsvalue. Inserting an existing value has no effect.["LOOKUP", value]emits"true"when the value is present and"false"otherwise.["REMOVE", value]removes the value when present and otherwise has no effect.["CLEAR"]removes every value.["ITERATE"]emits all current values in ascending lexicographic order, joined with commas. It emits the empty string when the set is empty.
Only LOOKUP and ITERATE emit outputs. Return those strings in command order.
Function
runSetCommands(operations: String[][]) → String[]Examples
Example 1
operations = [["PUT","pear"],["PUT","apple"],["LOOKUP","pear"],["ITERATE"],["REMOVE","pear"],["LOOKUP","pear"],["CLEAR"],["ITERATE"]]return = ["true","apple,pear","false",""]The first lookup succeeds. Iteration sorts apple before pear; after removal and clear, the later outputs show the updated state.
Example 2
operations = [["PUT","b"],["PUT","b"],["PUT","a"],["ITERATE"],["REMOVE","missing"],["LOOKUP","b"]]return = ["a,b","true"]Duplicate insertion and removal of an absent value do not change the set.
Constraints
1 <= operations.length <= 200000.- Every command is one of
PUT,LOOKUP,REMOVE,CLEAR, orITERATEand has the documented arity. - Every value has between
1and30ASCII letters, digits, or underscores. - The total number of characters across all commands is at most
2000000.