Skip to content

visual walkthrough

Simplify Path

MediumStack SimulationReported at: MetaAmazonGoogle+1

Path canonicalisation, a practical parsing task.

Solve on LeetCode

The idea

A path is a trip through directories. The directories you are inside form a stack: entering a folder pushes it, '..' leaves the most recent one (pops), and '.' or empty pieces do nothing.

The simplified path is whatever remains on the stack, joined with slashes.

Complexity

approachtimespace
Directory stackO(n)O(n)

More walkthroughs