Skip to content

Practice Simplify Path

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#71
LeetCode Problem

Simplify Path

Problem Statement

Convert an absolute Unix path to its canonical form. Treat repeated slashes as one separator, ignore a component equal to ., and use .. to move to the parent directory without moving above the root. Other component names are literal, including ... and ..... The result begins with one slash, uses one slash between retained components, and has no trailing slash unless the result is the root /.

Source: Simplify Path on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
path = "/home/"
Output
"/home"

# Example 2

Input
path = "/home//foo/"
Output
"/home/foo"

# Example 3

Input
path = "/home/user/Documents/../Pictures"
Output
"/home/user/Pictures"

# Example 4

Input
path = "/../"
Output
"/"

# Example 5

Input
path = "/.../a/../b/c/../d/./"
Output
"/.../b/d"

Constraints

1 <= path.length <= 3000
path consists of English letters, digits, period '.', slash '/' or '_'.
path is a valid absolute Unix path.

Related LeetCode Problems

Practice more problems similar to Simplify Path.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.