All problems

Directory Hopper

Medium · stack

A learner is practising on a text terminal. The folders form a tree: the root folder is written /, and every other folder is a path such as /home/me. The terminal starts in the home folder /home/me.

You are given a list of commands, run in order. Each command is one of:

A path is made of names separated by /:

Return the folder the terminal ends up in, written as a path that starts with /, has no . or .., no repeated slashes and no slash at the end (except for the root itself, which is /).

Examples

Example 1

Input: commands = ["cd ..", "cd docs/../notes"]

Output: "/home/notes"

Going up from /home/me gives /home. The path docs/../notes enters docs, leaves it again and enters notes.

Example 2

Input: commands = ["pushd /var/log", "cd ../tmp", "popd", "cd work"]

Output: "/home/me/work"

pushd remembers /home/me. After popd the terminal is back there, and cd work goes into work.

Example 3

Input: commands = ["cd /", "cd ../..", "pushd ~/a//b/.", "popd", "popd"]

Output: "/"

Going up from the root stays at the root. The pushd remembers / and popd returns to it. The last popd finds the folder stack empty and does nothing.

Constraints