Good evening! Here's our prompt for today.
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:
SNIPPET
1Absolute: /a/./b/../c
2Canonical: /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
SNIPPET
1Input: /a/./b/../../c
2Output: /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
SNIPPET
1Input: /a//b////c/d//././/..
2Output: /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.
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
36
console.log('PASSED: ' + "`simplifyAbsolutePath('/a//b//c//////d')` should return `/a/b/c/d`");
var assert = require('assert');
​
function simplifyAbsolutePath(path) {
// fill in the solution
return;
}
​
​
try {
assert.equal(simplifyAbsolutePath('/home/'), '/home');
console.log('PASSED: ' + "`simplifyAbsolutePath('/home/') should return `/home`");
} catch (err) {
console.log(err);
}
​
try {
assert.equal(simplifyAbsolutePath('/foo/../bar/x'), '/bar/x');
console.log('PASSED: ' + "`simplifyAbsolutePath('/foo/../bar/x')` should return `/bar/x`");
} catch (err) {
console.log(err);
}
​
try {
assert.equal(simplifyAbsolutePath('/a/./b/../../c/'), '/c');
console.log('PASSED: ' + "`simplifyAbsolutePath('/a/./b/../../c/')` should return `/c``");
} catch (err) {
console.log(err);
}
​
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's our guided, illustrated walk-through.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.