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:
ls():
Find the specified folder or file.
If directory, return the key set of the child directories and files, sorted.
If file, return name.
mkdir():
Find the specified folder or file, create new directories on the way if needed.
addContentToFile():
Find the specified file, add to its content.
readContentFromFile():
Find the specified file, get its content.
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.
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:
We can use this to create a general class for both folders and directories.
With this, each method reduces down to simple operations:
ls()
: Find the specified folder or file. If directory, return the key set of the child directories and files, sorted. If file, return name.mkdir()
: Find the specified folder or file, create new directories on the way if needed.addContentToFile()
: Find the specified file, add to its content.readContentFromFile()
: Find the specified file, get its content.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.