The problem
Convert an absolute Unix path (with ., .., //) into its canonical form: no trailing slash, no dots, single slashes.
Stuck? Reveal hints one at a time
How to approach it
- 1Split the path by "/".
- 2For each segment: skip empty and "."; on ".." pop the stack if non-empty; otherwise push the segment.
- 3Join the stack with "/" and prefix a single "/".
Key insight
".." undoes the most recent directory — "most recent first" is literally the stack contract.
The solution
Watch out for
- ".." at the root pops nothing — guard the pop.
- Names like "..." or "a..b" are VALID directory names, not dot operators.