Skip to main content

Simplify Path

MediumStringTree

Description

Given an absolute Unix path, simplify it to the canonical path. Handle '.', '..', and multiple slashes. Return the result as a string.

Examples

Input:path = "/a/./b/../../c/"
Output:"/c"
Explanation:

Canonical path after resolving . and ..

Input:/home//documents/../pictures/./vacation//
Output:"/home/pictures/vacation"
Explanation:

Multiple consecutive slashes are treated as single slashes, '..' moves up one directory (documents is removed), '.' refers to current directory (no change), and trailing slash is removed in the canonical form.

Input:/../../root/folder/
Output:"/root/folder"
Explanation:

When '..' tries to go above the root directory, it stays at root. The first two '..' operations have no effect since is being already at the filesystem root, then navigating to root/folder.

Constraints

  • 1 ≤ path.length ≤ 3000

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.