๐๏ธ Design In-Memory File System
Imagine you're building a cloud storage system that needs to simulate a complete file system entirely in memory. You need to create a data structure that can handle all the basic operations you'd expect from a real file system: listing directories, creating folders, writing files, and reading their contents.
Your FileSystem class should support these operations:
- List Directory/File (
ls): Given a path, return all files and folders in lexicographic order. If it's a file, return just that filename. - Create Directory (
mkdir): Create a new directory (and any missing parent directories) at the given path. - Write/Append to File (
addContentToFile): Create a new file with content, or append to existing file content. - Read File (
readContentFromFile): Return the complete content of a file.
Think of it like implementing your own version of a simplified Unix file system commands!
Input & Output
Visualization
Time & Space Complexity
Where L is the length of the path (number of directories in path). Each operation only needs to traverse the path once.
Where N is total number of files/directories and L is average path depth. Shared prefixes save space compared to storing full paths.
Constraints
- 1 โค path.length, filePath.length โค 100
- 1 โค content.length โค 50
- All paths are absolute paths starting with '/' and do not end with '/' except for root
- At most 300 calls will be made to the methods
- File and directory names consist of only lowercase English letters and digits