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.

In Unix-style systems, we can define the path to a file or directory in two ways; absolute path and canonical path.

An absolute path specifies the location of a file or directory from the root directory and begins with a single or multiple slash / characters. It can also have a period '.' (referring to the current directory), a double period '..' (refers to the directory up a level), or trailing and multiple consecutive slashes '//' (treated as a single slash '/') in the path.

A canonical path is a simplified form of an absolute path. Its components are all names of real directories or files. This means that it excludes ., .., trailing, and repeated slash characters.

Now consider that you are given a string path(an absolute path), to a file or directory in a Unix-style file system. Convert it to the simplified canonical path.

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

For this particular problem, any other format of periods such as '...' is treated as file/directory names.

Let's consider an example. Suppose the path given is /a/./b/. According to the path, first, we get to directory 'a', so we add the same to our canonical path. Next, we encounter a single period, so we do not make any changes to the canonical path and move on (a single period '.' indicates the current directory, so we do not need to add anything to our canonical path). We move to the next directory 'b' and add it to the canonical path. This gives our final answer.

Constraints

  • 1 <= path.length <= 3000
  • path consists of English letters, digits, period '.', slash '/' or '_'.
  • path is a valid absolute Unix path.

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

Challenges • Asked 6 months 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).