Similar Problems
Similar Problems not available
Design File System - Leetcode Solution
LeetCode: Design File System Leetcode Solution
Difficulty: Medium
Topics: string design hash-table
Design File System problem on LeetCode revolves around designing a data structure that can simulate a file system with directories and files. The problem is focused on designing the structure and methods that can allow adding files to directories, creating directories, and updating or fetching files.
The given prompt requires us to implement two actions:
- Creating a simple file system where we will be adding files and directories.
- We will fetch or get the value of the file or directory.
Now, let's dive into the solution of the problem.
To solve this problem, we can use a data structure called Trie for efficient storage and retrieval of files and directories. The Trie data structure is efficient for this type of problem because it stores data in a tree-like structure, which makes searching and retrieval of data convenient and fast.
We can use a Trie data structure with a node class, which contains:
- A dictionary to store the child nodes,
- A flag to indicate if the current node is a file or a directory,
- A size attribute to store the size of the current node.
The main class will have a root
node to begin the trie search and add files and directories. To add a file, we will traverse the path of directories, create the directory nodes if they do not already exist. When we reach the end of the path, we will add the file to the directory.
Similarly, to add a directory, we will traverse the path of directories, create the directory nodes if they do not already exist. After creating the directories, we will mark the last node in the path as a directory.
To fetch a file or directory, we will traverse the path of directories, return None if any of the directory nodes does not exist; otherwise, we will return the last node in the path.
Here's the implementation of the solution in Python:
Node Class:
class Node:
def __init__(self):
self.children = {}
self.isFile = False
self.size = 0
FileSystem Class:
class FileSystem:
def __init__(self):
self.root = Node()
def createPath(self, path):
node = self.root
dirs = path.split('/')
for directory in dirs[1:]:
if directory not in node.children:
node.children[directory] = Node()
node = node.children[directory]
return node
def createFile(self, filePath, fileSize):
node = self.createPath(filePath)
if not node.isFile:
node.isFile = True
node.size = fileSize
return True
return False
def createDir(self, dirPath):
node = self.createPath(dirPath)
if not node.isFile:
node.size = 0
return True
return False
def get(self, path):
node = self.createPath(path)
if not node:
return -1
if node.isFile:
return node.size
return 0
In the createPath
method, we traverse the Trie tree by splitting the path into directories, and then we create the nodes for the directories that do not exist.
In the createFile
method, we create the path and add the file to the last node in the path. If the last node is already a file, then we return false; otherwise, we set the node's flag to True
, add the file's size to the node, and return True.
In the createDir
method, we create the path and mark the last node as a directory. If the last node is already a file, then we return false, otherwise we set the size of the directory to 0, and return True.
In the get
method, we return -1 if the path cannot be created. If the node represents a file, we return the size of the file, otherwise 0.
In conclusion, we can solve the Design File System problem on LeetCode by using the Trie data structure with a node class to efficiently store and retrieve files and directories. We can use createPath
method to traverse the Trie tree, createFile
to add files, createDir
to add directories, and get
method to fetch files or directories.
Design File System Solution Code
1