603. Simplify Unix Directory Path

EasyStackStackString

Given an absolute Unix-style file path, simplify it to its canonical form: '.' means the current directory, '..' moves up one level, and multiple slashes collapse to one. The canonical path starts with a single '/' and has no trailing slash (except the root). Return the simplified path. The input is JSON {path}.

Input: JSON {path}.

Output: String — the canonical path.

Examples

Example 1
Input: {"path":"/a/./b/../../c/"}
Output: /c
Explanation: Resolves to /c.
Example 2
Input: {"path":"/../"}
Output: /
Explanation: Cannot go above root.
Example 3
Input: {"path":"/home//foo/"}
Output: /home/foo
Explanation: Collapses double slashes.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →