588. Design In-Memory File System
Design an in-memory file system with the following operations:
ls(path): If path is a file, return its name. If path is a directory, return all files and directories in 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","ls","mkdir","addContentToFile","ls","readContentFromFile"],[[],["/"],["/a/b/c"],["/a/b/c/d","hello"],["/"],["/a/b/c/d"]]]
Output: [null,[],null,null,["a"],"hello"]
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"]
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: