#K62192. Simplify Unix File Path

    ID: 31477 Type: Default 1000ms 256MiB

Simplify Unix File Path

Simplify Unix File Path

You are given a string s that represents a file path in a Unix-style file system. Your task is to simplify the path and return the shortest possible equivalent absolute path.

The path may contain directory names, the current directory symbol ., and the parent directory symbol ... Multiple consecutive slashes are considered as a single separator. For example, in a Unix file system, the path /a/./b/../../c/ should be simplified to /c.

The algorithm should work as follows:

  • Split the input string by the slash character / to identify components.
  • Ignore empty components and any occurrences of ..
  • For each occurrence of .., remove the last valid directory from the result (if one exists).
  • Append any valid directory name to the result.
  • The simplified path should always begin with a slash / and there should not be any redundant slashes.

In mathematical terms, let the input path be represented as a sequence P = [p_1, p_2, \ldots, p_n] where each component is processed according to the rules:

$$\text{if } p_i = '.' \text{ or } p_i = '' \text{, skip}; $$$$\text{if } p_i = '..' \text{, remove the last valid directory from the stack (if exists)}; $$otherwise, add pi to the stack.\text{otherwise, add } p_i \text{ to the stack.}

Finally, the output path is given by joining the remaining stack components with /, and prepending a / to it.

inputFormat

The input consists of a single line containing the file path s.

You can assume that the input path is a valid Unix-style path.

outputFormat

Print the simplified Unix-style absolute file path.

## sample
/home/
/home