Problem
Simplify the absolute Unix-style path absolute_path into its canonical form by applying these path-component rules:
-
Repeated slashes act as one separator.
-
A component equal to
.
refers to the current directory.
-
A component equal to
..
moves to the parent directory. At the root, moving to a parent leaves you at the root.
-
Other names are ordinary components. In particular,
...
and longer runs of periods are names, not parent-directory instructions.
Return a path beginning with exactly one slash. Use one slash between remaining components and omit a final slash unless the result is the root path /. The result must contain no . or .. navigation components.
Examples
Example 1
Input: absolute_path = "/var//logs/./app/../cache/"
Output: "/var/logs/cache"
The repeated separator and current-directory component disappear, and the parent-directory component cancels app.
Example 2
Input: absolute_path = "/../../.../x/.."
Output: "/..."
The first two parent moves stay at the root. The name ... remains, while the final parent move cancels x.
Constraints
-
1 <= absolute_path.length <= 3000
-
The string is a valid absolute Unix path and starts with
/
.
-
It contains only English letters, decimal digits,
.
,
/
, and
_
.