232. Simplify Path

MediumStringString

Given an absolute Unix-style file path, simplify it to its canonical form. Resolve '.' (current), '..' (parent), and collapse multiple slashes. The result starts with a single '/' and has no trailing slash (except root).

Input: A quoted path string.

Output: Quoted string — the canonical path.

Examples

Example 1
Input: "/home/"
Output: "/home"
Explanation: Trailing slash removed.
Example 2
Input: "/../"
Output: "/"
Explanation: Cannot go above root.
Example 3
Input: "/a/./b/../../c/"
Output: "/c"
Explanation: Resolved to /c.

Constraints

Asked by

MetaOracleMicrosoftAmazonBloombergGoogle
Solve this problem in the editor →