WorkspaceProblem lab
ALGORITHM VISUALIZER

Simplify Unix Path

Opening the interactive workspace…

Understanding Simplify Unix Path

Normalize an absolute Unix-style path by removing repeated separators and dot components, and resolving parent components.

How it works

A stack stores the directory components that remain after processing each segment.

Split the path on slashes. Ignore empty and dot components; pop for a parent component. Push ordinary names and join the remaining stack with a leading slash.

Try an example

Input: path = "/a/./b/../../c/"

Output: "/c"

A stack stores the directory components that remain after processing each segment.

Constraints

  • Path starts with / and contains at most 128 characters.
  • Allowed characters are ASCII letters, digits, underscore, hyphen, dot, and slash.
  • Only exact . and .. components are special; going above root stays at root.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity