588. Design In-Memory File System
Design an in-memory file system with the following operations:
ls(path): If path is a file, return a list containing only that file's name. If path is a directory, return the names of all files and directories directly inside it, sorted lexicographically.mkdir(path): Create a directory at path if it doesn't exist (path may have nested directories).addContentToFile(filePath, content): Create a file at filePath with given content if it doesn't exist, otherwise append content.readContentFromFile(filePath): Return the content of the file.
Examples
Input: [["FileSystem","mkdir","addContentToFile","ls","readContentFromFile","addContentToFile","readContentFromFile","ls"],[[],["/a/d0"],["/a/d0/f0","x0"],["/a"],["/a/d0/f0"],["/a/d0/f0","y"],["/a/d0/f0"],["/a/d0"]]]
Output: [null,null,null,["d0"],"x0",null,"x0y",["f0"]]
Input: [["FileSystem","mkdir","addContentToFile","ls","readContentFromFile","addContentToFile","readContentFromFile","ls"],[[],["/b/d1"],["/b/d1/f1","x1"],["/b"],["/b/d1/f1"],["/b/d1/f1","y"],["/b/d1/f1"],["/b/d1"]]]
Output: [null,null,null,["d1"],"x1",null,"x1y",["f1"]]
Hints
Use a Trie-like structure where each node represents a file or directory.
Store both children (directories) and content (for files) in each node.
For `ls`, parse the path and traverse the trie to find the target node.
Related Problems
588. Design In-Memory File System
Design an in-memory file system with the following operations: