Codewriting In Unix, there are two common ways to execute a command: * Entering its…
- User
Codewriting
In Unix, there are two common ways to execute a command:
- Entering its name, e.g., "cp" or "ls"
- Entering "!". This notation is used to repeat the
indexth (1-based) command since the start of the session. For example, suppose that the user has entered the following commands:
ls cp mv mv mv !1 !3 !6
"!1" would trigger the execution of "ls", "!3" would execute "mv", and "!6" would execute "!1" which in turn would trigger the execution of "ls".
You are given a sequence of commands the terminal since the start of the session. Each command can be one of the following: "cp", "ls", "mv" or "!". Calculate the number of times each of "cp", "ls", and "mv" commands was executed and return an array of three integers in the following form: [# of times for "cp", # of times for "ls", # of times for "mv"]
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(commands.length²) will fit within the execution time limit.
Example
- For commands = ["ls", "cp", "mv", "mv", "mv","!1","!3", "!6"], the output should be solution(commands) = [1, 3, 4].
Explanation:
- First, "ls" was executed once.
- Then, "cp" was executed once.
- After that, "mv" was executed three times.
- Then, "!1" was executed, triggering the execution of commands[0] = "ls".
- Then, "!3" was executed, triggering commands[2] = "mv".
- Finally, "!6" was executed, triggering commands[5] = "!1", which in turn triggered commands[0] = "ls".
In total, "cp" was executed once, "ls" was executed three times, and "mv" was executed four times, so the final answer is [1, 3, 4].
Input/Output
- [execution time limit] 0.5 seconds (cpp)
- [memory limit] 1 GB
- [input] array.string commands
solve in cpp
- Assistant