visual walkthrough
Simplify Path
Path canonicalisation, a practical parsing task.
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
| approach | time | space |
|---|---|---|
| Directory stack | O(n) | O(n) |