Build a tiny file system that lives in memory. It starts with just the root directory /, and holds directories and text files. Every path is absolute: / alone is the root, and /docs/notes/todo names todo inside notes inside docs. Implement the FileSystem class:
FileSystem() creates a file system with an empty root directory.ls(path) lists names:
path is a directory, it returns the names of everything directly inside it (files and directories alike, not their contents), sorted in lexicographic order;path is a file, it returns a list holding only that file's name.mkdir(path) creates the directory path, along with any directories on the way to it that don't exist yet.addContentToFile(filePath, content) appends content to the file filePath. If there is no such file yet, it creates it first, empty.readContentFromFile(filePath) returns everything written to the file filePath so far.Each example lists the calls in order, then the arguments of each call, then what each call returns: null for the constructor, mkdir and addContentToFile, which return nothing.
Example 1
Input:
["FileSystem", "mkdir", "addContentToFile", "addContentToFile", "ls", "ls", "readContentFromFile"]
[[], ["/music/jazz"], ["/music/list", "blue"], ["/music/list", "train"], ["/music"], ["/music/list"], ["/music/list"]]
Output: [null, null, null, null, ["jazz", "list"], ["list"], "bluetrain"]
mkdir creates music on the way to jazz. The second write appends to the file the first one created. Listing /music shows both entries in sorted order; listing the file itself shows just its own name.
Example 2
Input:
["FileSystem", "ls", "mkdir", "mkdir", "addContentToFile", "ls", "ls"]
[[], ["/"], ["/x/yz"], ["/x/y"], ["/x/b", "ok"], ["/x"], ["/x/yz"]]
Output: [null, [], null, null, null, ["b", "y", "yz"], []]
The root starts empty. y sorts before yz because it is a prefix of it, and an empty directory lists as an empty list.
Constraints
1 <= path.length, filePath.length <= 100path and filePath starts with / and does not end with /, except path may be exactly "/". Names between slashes are non-empty and made of lowercase English letters.1 <= content.length <= 50, and content is made of lowercase English letters.ls is only given a path that exists; mkdir is only given a path that does not exist yet and does not run through a file; addContentToFile is only given a path whose parent directory already exists and that is not a directory; readContentFromFile is only given an existing file.300 calls are made in total to ls, mkdir, addContentToFile and readContentFromFile.Model it as a tree. Each node is a directory or a file, and a directory maps each child's name to its node. Every operation starts at the root and walks the path one name at a time.
["FileSystem","mkdir","addContentToFile","addContentToFile","ls","ls","readContentFromFile"] [[],["/music/jazz"],["/music/list","blue"],["/music/list","train"],["/music"],["/music/list"],["/music/list"]]
[null,null,null,null,["jazz","list"],["list"],"bluetrain"]