11/36
Part 3 of 8 · Open and closeLearn and do

Input that opens and closes

The same idea parses text with levels in it. Whenever part of the input opens something that a later part closes, the open things wait on a stack. A .. in a path cancels the directory entered last. A ] closes the last [. In part 5, a profiler's end event closes the start it matches.

.. pops the directory entered last

Simplify Path is on the compiled LeetCode list for OpenAI (1 source). Turn a Unix path into its shortest form, where . means this directory and .. means the parent.

Split the path on /, and walk the parts with a stack of directory names. Skip an empty part and .. Pop for .., unless the stack is already empty. Push any other name. At the end, join the stack with / behind a leading /.

Predict

What's the shortest form of /srv//app/./logs/../cache/?

Pick an answer above to see the rest