Simplify Path

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Simplify Path

Approach

Split the path by slash and process tokens with a stack. Ignore empty and dot tokens, pop for double-dot tokens when possible, and push normal directory names. Join stack contents with slashes for the canonical path.

Complexity Analysis

Time
O(n)
Space
O(n)

Pattern

Stack Path Normalization

Why It Works

Each token updates directory state locally: entering pushes, parent traversal pops. The stack always represents the current canonical absolute path.

Updated Feb 2026