Shell Command Counts with History References
Problem statement
Process the entries of history in order. Each entry is a base command cp, ls, or mv, or a history reference !index.
A reference executes the same base command that was executed by the entry at the one-based position index. That earlier entry may itself be a reference. Each history entry executes exactly one base command.
Return the total execution counts in the order [cp, ls, mv], including direct commands and references.
For this exercise, assume every reference points to an earlier entry and uses a positive decimal index without leading zeros. All input entries are valid.
Function
countShellCommands(history: String[]) → int[]Examples
Example 1
history = ["cp","ls","!1","mv","!3"]return = [3,1,1]The executed commands are cp, ls, cp, mv, and cp. The last entry references an earlier reference.
Example 2
history = ["mv","!1","!2","ls"]return = [0,1,3]The first three entries all execute mv; the fourth executes ls.
Constraints
- For this exercise, assume
1 <= history.length <= 10^5. - Each entry is
cp,ls,mv, or!index. - For a reference at one-based position
i,1 <= index < i.