Button Combination Detector
Problem statement
Button Combination Detector
A device produces a finite sequence of button events. Every event has the form down:BUTTON or up:BUTTON, where BUTTON is a case-sensitive label.
No buttons are pressed initially. Process the events in order and return true if, after any event, every label in requiredButtons is pressed at the same time. Other buttons may also be pressed. Return false if the required combination never occurs.
Pressing an already pressed button or releasing a button that is not pressed is a valid no-op.
Function
wasButtonCombinationPressed(events: String[], requiredButtons: String[]) → booleanExamples
Example 1
events = ["down:A","down:B","up:A"]requiredButtons = ["A","B"]return = trueAfter processing down:B, both A and B are pressed, so the required combination occurs.
Example 2
events = ["down:A","up:A","down:B","down:A"]requiredButtons = ["A","B"]return = trueThe first press of A is released before B is pressed. The final event presses A again while B is still down.
Example 3
events = ["down:A","down:C","up:C"]requiredButtons = ["A","B"]return = falseButton B is never pressed, so the required combination never occurs.
Constraints
1 ≤ events.length ≤ 100,0001 ≤ requiredButtons.length ≤ 100- Every required label is unique and matches
[A-Za-z0-9_-]{1,20}. - Every event is exactly
down:BUTTONorup:BUTTONfor a valid label.