Design a data structure that simulates an in-memory file system.
Implement the FileSystem class:
FileSystem() Initializes the object of the system.List<String> ls(String path)
path is a file path, returns a list that only contains this file's name.path is a directory path, returns the list of file and directory names in this directory.void mkdir(String path) Makes a new directory according to the given path. The given directory path does not exist. If the middle directories in the path do not exist, you should create them as well.void addContentToFile(String filePath, String content)
filePath does not exist, creates that file containing given content.filePath already exists, appends the given content to original content.String readContentFromFile(String filePath) Returns the content in the file at filePath.
Example 1:
Input
["FileSystem", "ls", "mkdir", "addContentToFile", "ls", "readContentFromFile"]
[[], ["/"], ["/a/b/c"], ["/a/b/c/d", "hello"], ["/"], ["/a/b/c/d"]]
Output
[null, [], null, null, ["a"], "hello"]
Explanation
FileSystem fileSystem = new FileSystem();
fileSystem.ls("/"); // return []
fileSystem.mkdir("/a/b/c");
fileSystem.addContentToFile("/a/b/c/d", "hello");
fileSystem.ls("/"); // return ["a"]
fileSystem.readContentFromFile("/a/b/c/d"); // return "hello"
Constraints:
1 <= path.length, filePath.length <= 100path and filePath are absolute paths which begin with '/' and do not end with '/' except that the path is just "/".addContentToFile will exist.1 <= content.length <= 50300 calls will be made to ls, mkdir, addContentToFile, and readContentFromFile.Java
Go
19. Design File System using Composite Design Pattern | Low Level Design Interview Question | LLD • Concept && Coding - by Shrayansh • 52,650 views views
Watch 9 more video solutions →Practice Design In-Memory File System with our built-in code editor and test cases.
Practice on FleetCode