Recursively Expanded Shell Command Counts
Problem statement
You are given a command history. Every entry is one of the base commands cp, ls, and mv, or a history reference of the form !index.
Each index is one-based, refers to an earlier entry, and executes the command represented by that entry. A reference may point to another reference, so resolve references recursively.
Return the total execution counts in the order [cp, ls, mv], counting both direct executions and executions reached through references.
Function
countShellCommands(history: String[]) → int[]Examples
Example 1
history = ["ls","cp","!1","!3","mv"]return = [1,3,1]Entries 1, 3, and 4 execute ls. The direct cp and mv entries execute once each.
Example 2
history = ["cp","!1","!2"]return = [3,0,0]Both references ultimately resolve to cp, so all three entries execute that command.
Example 3
history = ["mv","ls","cp","!2","!1","!4"]return = [1,3,2]The final reference points to entry 4, which resolves to ls. The totals are one cp, three ls executions, and two mv executions.
Constraints
1 <= history.length <= 10^5.- Every entry is
cp,ls,mv, or!index. - Every referenced
indexis between1and the current entry's one-based position minus1.