*588. Design In-Memory File System

https://leetcode.com/problems/design-in-memory-file-system/

solution

class TrieNode:
    def __init__(self):
        self.name = None
        self.is_file = False
        self.content = []
        self.children = {}
  
    def insert(self, path: str, is_file: bool) -> 'TrieNode':
        # Insert a path into the Trie and return the final node
        node = self
        parts = path.split('/')
        for part in parts[1:]:  # Skip empty root part
            if part not in node.children:
                node.children[part] = TrieNode()
            node = node.children[part]
        node.is_file = is_file
        if is_file:
            node.name = parts[-1]
        return node
  
    def search(self, path: str) -> 'TrieNode':
        # Search for a node given a path in the Trie
        node = self
        if path == '/':
            return node
        parts = path.split('/')
        for part in parts[1:]: # Skip empty root part
            if part not in node.children:
                return None
            node = node.children[part]
        return node

class FileSystem:
    def __init__(self):
        self.root = TrieNode()

    def ls(self, path: str) -> List[str]:
        # List directory or file
        node = self.root.search(path)
        if node is None:
            return []
        if node.is_file:
            # If it's a file, return a list with its name
            return [node.name]
        # If it's a directory, return the sorted list of children's names
        return sorted(node.children.keys())

    def mkdir(self, path: str) -> None:
        # Create a directory given a path
        self.root.insert(path, False)

    def addContentToFile(self, filePath: str, content: str) -> None:
        # Add content to a file, creating the file if it doesn't exist
        node = self.root.insert(filePath, True)
        node.content.append(content)

    def readContentFromFile(self, filePath: str) -> str:
        # Read content from a file
        node = self.root.search(filePath)
        if node is None or not node.is_file:
            raise FileNotFoundError(f"File not found: {filePath}")
        return ''.join(node.content)

时间复杂度:O() 空间复杂度:O()

Last updated