spiralgo / algorithms

We voluntarily solve algorithm problems on this repo.
24 stars 7 forks source link

588. Design In-Memory File System #387

Closed ErdemT09 closed 3 years ago

ErdemT09 commented 3 years ago

Resolves: #226

I recommend checking out #188 before this problem, it's kind of like the simpler version.

Algorithm:

Each file/directory here has few properties:

- name
- child directories and files as a map (for directories)
- content (for files)
- is file = (content = null)

We can use this to create a general class for both folders and directories.

With this, each method reduces down to simple operations:

The common problem here is finding the file or the directory. Hence, it's only most convenient to create a separate method for it. We simply get the path name and split it into strings originally separated by '/'. From the root directory we are keeping, we simply traverse down to the sought folder using the maps of the directories. We also create new directories when we can't find the directory name we had in the paths string.