Simplifying Absolute Paths
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Question
Here is how I would enrich the absolute path simplification problem with more details and walkthroughs:
Simplifying Absolute Paths
In Unix, an absolute path locates a file/directory from the root directory. It can contain:
-
.- Stands for the current directory -
..- Stands for the parent directory - Multiple
/- Treated as a single/
A canonical path simplifies an absolute path by:
- Removing
.,..and extra/ - Keeping only real directory/file names
For example:
Absolute: /a/./b/../c
Canonical: /a/c
Here we removed:
- . - Current directory in /a/./b
- .. - Goes up from /b to /a
- Extra / between a and b
Leaving us with the simplified /a/c
Problem
Given an absolute path string, convert it to its canonical form.

Example 1
Input: /a/./b/../../c
Output: /c
Walkthrough: - /a/./b - Remove . - /a/b/../ - Go up from b to a - /a/../c - Go up from a to root - /c - Final simplified canonical path
Example 2
Input: /a//b////c/d//././/..
Output: /a/b/c
Walkthrough:
- /a//b/ - Remove extra /
- /a/b/c/d//./. - Remove . and extra /
- /a/b/c/d/.. - Go up from d to c
- /a/b/c - Final simplified path
Constraints
- 1 <= path.length <= 3000
- path contains letters, digits, . / or _
- path is a valid Unix absolute path
The goal is to take an absolute path string and simplify it into its canonical form.
CONTINUE WITH ALGODAILY PREMIUM
Keep going with the complete tutorial.
Unlock the remaining explanations and exercises, alongside the full learning library.
Explore Premium →