Medium · Stacks & Queues

Simplify Path

Given an absolute Unix-style path, return its simplified canonical path: a single leading slash followed by the remaining directory names separated by single slashes, where empty parts and "." are ignored and each ".." removes the previous name (doing nothing at the root).

Examples

Example 1

{
  "path": "/a/./b/../../c/"
}

Output: /c

Example 2

{
  "path": "/home//foo/"
}

Output: /home/foo

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Stacks & Queues problems