FastPrepRecursively Expanded Shell Command Counts

Recursively Expanded Shell Command Counts

Hudson River Trading logoHudson River Trading● EasyNEW GRADOA
Learn

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 index is between 1 and the current entry's one-based position minus 1.

More Hudson River Trading problems

See Hudson River Trading hiring insights
public int[] countShellCommands(String[] history) {
    // Write your code here.
}
history["ls","cp","!1","!3","mv"]
expected[1,3,1]
Checking account…