Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Simplifying Absolute Paths (Main Thread)

Here is the interview question prompt, presented for reference.

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.

![image](https://storage.googleapis.com/algodailyrandomassets/curriculum/easy-strings/simplifying-absolute-paths/problem.png)

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.

You can see the full challenge with visuals at this link.

Challenges • Asked about 2 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Oct 02, 2022:

This is the main discussion thread generated for Simplifying Absolute Paths (Main Thread).