mediumStackStringStack

Simplify Path

mediumTime: O(n)Space: O(n)

Signals to notice

simplify Unix pathhandle . and .. and //stack for directory hierarchy

Brute force first

Not applicable — stack IS the natural approach.

The key insight

Split by '/'. For each part: '.' = skip, '..' = pop, '' = skip, else push. Join with '/' prefix. O(n).

What must stay true

The stack represents root-to-current path. '..' = go up (pop). Valid names = go deeper (push).

Easy way to go wrong

Popping empty stack on '..' — stay at root (don't pop).

Stack Pattern