LeetCode 588: Design In-Memory File System Solution

Master LeetCode problem 588 (Design In-Memory File System), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

588. Design In-Memory File System

Problem Explanation

Explanation:

To design an in-memory file system, we can use a tree-like structure where each node represents either a file or a directory. We can implement this by creating a File class which can be either a file or a directory. Each directory can contain multiple files or directories.

We will maintain a root directory as the starting point of our file system. We can use a hashmap to store the children of each directory. Each file or directory will have a name and a reference to its parent directory.

Our main operations will be creating a file or directory, reading the content of a file, and listing the files in a directory. We can use a Trie data structure to efficiently store and retrieve the content of files.

Time Complexity:

  • Creating a file or directory: O(n) where n is the length of the file path
  • Reading the content of a file: O(1) if we store the content in a hashmap, O(n) if we use a Trie
  • Listing files in a directory: O(k) where k is the number of files/directories in the directory

Space Complexity:

  • O(n) where n is the total number of files and directories in the file system

:

Solution Code

// Java Solution
class File {
    boolean isFile;
    HashMap<String, File> children;
    String content;
    
    public File() {
        isFile = false;
        children = new HashMap<>();
        content = "";
    }
}

class FileSystem {
    File root;

    public FileSystem() {
        root = new File();
    }
    
    public List<String> ls(String path) {
        String[] dirs = path.split("/");
        File node = root;
        List<String> result = new ArrayList<>();
        String name = "";
        for (String dir : dirs) {
            if (dir.length() == 0) continue;
            if (!node.children.containsKey(dir)) return result;
            node = node.children.get(dir);
            name = dir;
        }
        
        if (node.isFile) {
            result.add(name);
        } else {
            for (String key : node.children.keySet()) {
                result.add(key);
            }
        }
        
        Collections.sort(result);
        return result;
    }
    
    public void mkdir(String path) {
        String[] dirs = path.split("/");
        File node = root;
        for (String dir : dirs) {
            if (dir.length() == 0) continue;
            if (!node.children.containsKey(dir)) {
                node.children.put(dir, new File());
            }
            node = node.children.get(dir);
        }
    }
    
    public void addContentToFile(String filePath, String content) {
        String[] dirs = filePath.split("/");
        File node = root;
        for (String dir : dirs) {
            if (dir.length() == 0) continue;
            if (!node.children.containsKey(dir)) {
                node.children.put(dir, new File());
            }
            node = node.children.get(dir);
        }
        node.isFile = true;
        node.content += content;
    }
    
    public String readContentFromFile(String filePath) {
        String[] dirs = filePath.split("/");
        File node = root;
        for (String dir : dirs) {
            if (dir.length() == 0) continue;
            if (!node.children.containsKey(dir)) return "";
            node = node.children.get(dir);
        }
        return node.content;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 588 (Design In-Memory File System)?

This page provides optimized solutions for LeetCode problem 588 (Design In-Memory File System) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 588 (Design In-Memory File System)?

The time complexity for LeetCode 588 (Design In-Memory File System) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 588 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 588 in Java, C++, or Python.

Back to LeetCode Solutions