Mark As Completed Discussion

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.

Question

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?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's our guided, illustrated walk-through.

How do I use this guide?