15% off all plans & roadmaps Use code
See plansLeetCode 71 · Solution & editorial
You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path.
Simplify Path is a Stack Patterns problem, in the Simulation / Backtracking Helper subpattern. The pattern covers parentheses matching, monotonic stack, histogram, and expression evaluation
Use stack for simulation problems like simplify path, decode string, and asteroid collision.
The reference solution runs in O(n) time and O(n) space.
Simplify Path has been reported in interviews at Facebook, Google, Amazon, Microsoft.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Simplify Path is a Stack Patterns problem, in the Simulation / Backtracking Helper subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.