You are given path, an absolute path (it starts with '/') to a file or folder in a Unix-style file system. Return the shortest correct form of the same path, called the canonical path.
These rules apply:
- A single dot
'.' means the current folder. Skip it.
- Two dots
'..' mean go up one folder. At the root folder, going up does nothing.
- Many slashes in a row, like
'//', count as one slash.
- Any other run of dots, such as
'...', is a normal folder or file name.
The canonical path must follow these rules:
- It starts with one slash
'/'.
- Two folder names are separated by exactly one slash.
- It does not end with a slash (except when the path is just the root).
- It has no
'.' or '..' parts.
Examples
Example 1:
Input: path = "/usr/local/../bin/./"
Output: "/usr/bin"
Explanation: ".." cancels "local", and "." is skipped.
Example 2:
Input: path = "/a/b/../../.."
Output: "/"
Explanation: Going up from the root does nothing, so the result is the root.
Example 3:
Input: path = "/x/.../y//"
Output: "/x/.../y"
Explanation: "..." is a normal name, and the double slash counts as one.