dsa · medium

Simplify Path

Given an absolute Unix path path (it starts with /), return the canonical path.

Arguments

A canonical path starts with /, has no trailing slash (unless it is the root /), and never contains . or .. as path segments. Extra slashes (//) collapse. . is the current directory and is dropped. .. moves to the parent; at the root it stays at /. Any other segment (including "...") is a real directory name.

Example

"/home/""/home" (drop the trailing slash).

"/../""/" (parent of root is still root).

"/home//foo/""/home/foo" (collapse the empty segment between the two slashes).

Constraints

1 <= path.length <= 3000 path is an absolute Unix path: it starts with / and contains letters, digits, `_`, `.`, and `/` Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
"/home/"

Expected:
/home

Example 2

Input:
"/../"

Expected:
/

Example 3

Input:
"/home//foo/"

Expected:
/home/foo

Open in the Dojo editor