Medium · Stack Normalisation
Understand → Visualise → Practice.
Return the canonical absolute Unix path after resolving repeated separators, dots and parent-directory steps.
Each part of the path is a directory name, '.', '..' or empty. Directory names are pushed, '..' pops, and the rest are ignored.
Split the path on '/'; repeated slashes give empty parts.
Push names; on '..' pop if the stack isn't empty; skip '' and '.'.
Return '/' joined with the stack, which gives '/' when the stack is empty.
O(n) time and O(n) space.
'...' is an ordinary directory name, not a special move.
Read the full guides first: Stacks, Queues and Deques.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.