Skip to content

Commit 81e3f5b

Browse files
author
Partho Biswas
committed
588_Design_In_Memory_File_System
1 parent 6409010 commit 81e3f5b

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
489489
|02| [211. Add and Search Word. Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)| [Python](https://tinyurl.com/wu6rdaw/211_Add_and_Search_Word_Data_structure_design.py)| [Video 01](https://www.youtube.com/watch?v=qi2ohSEyyDw), [Video 02](https://www.youtube.com/watch?v=neb_2UK5Kuo&t=13s)|
490490
|03| [642. Design Search Autocomplete System](https://leetcode.com/problems/design-search-autocomplete-system/)| [Python](https://tinyurl.com/wu6rdaw/642_Design_Search_Autocomplete_System.py)| [Article 01](https://leetcode.com/problems/design-search-autocomplete-system/discuss/105386/Python-Clean-Solution-Using-Trie), [Video 01](https://www.youtube.com/watch?v=us0qySiUsGU)| Hard | --- |
491491
|04| [472. Concatenated Words](https://tinyurl.com/y9cv6dk5)| [Python](https://tinyurl.com/wu6rdaw/472_Concatenated_Words.py)| [Art 01](https://tinyurl.com/ycjm6fuo), [Art 02](https://tinyurl.com/ybvl3kbv)| Hard | Very important |
492+
|05| **[588. Design In-Memory File System](https://tinyurl.com/ya238cp4)** | [Python](https://tinyurl.com/wu6rdaw/588_Design_In_Memory_File_System.py)| --- | Hard | --- |
492493

493494
</p>
494495
</details>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.childDirSet = set()
5+
self.name = None
6+
self.fileContent = ""
7+
8+
9+
class Trie:
10+
def __init__(self):
11+
self.root = TrieNode()
12+
self.endSymbol = "/"
13+
14+
def addFileContentToPath(self, path, content):
15+
currentNode = self.root
16+
directories = path.split("/")
17+
for i in range(1, len(directories)):
18+
directorie = directories[i]
19+
currentNode.childDirSet.add(directorie)
20+
for char in directorie:
21+
if char not in currentNode.children:
22+
currentNode.children[char] = TrieNode()
23+
currentNode = currentNode.children[char]
24+
currentNode.name = directorie
25+
if content:
26+
currentNode.childDirSet.add(currentNode.name)
27+
currentNode.fileContent += content
28+
29+
def addPath(self, path):
30+
self.addFileContentToPath(path, None)
31+
32+
def readContentFromPath(self, path):
33+
currentNode = self.getLastNode(path)
34+
return currentNode.fileContent
35+
36+
def getCurrentDirContent(self, path):
37+
currentNode = self.getLastNode(path)
38+
dirList = list(currentNode.childDirSet)
39+
dirList.sort()
40+
return dirList
41+
42+
def getLastNode(self, path):
43+
currentNode = self.root
44+
directories = path.split("/")
45+
for i in range(1, len(directories)):
46+
currentDir = directories[i]
47+
for char in currentDir:
48+
currentNode = currentNode.children[char]
49+
return currentNode
50+
51+
52+
class FileSystem(object):
53+
54+
def __init__(self):
55+
self.trie = Trie()
56+
57+
def ls(self, path):
58+
"""
59+
:type path: str
60+
:rtype: List[str]
61+
"""
62+
return self.trie.getCurrentDirContent(path)
63+
64+
def mkdir(self, path):
65+
"""
66+
:type path: str
67+
:rtype: None
68+
"""
69+
self.trie.addPath(path)
70+
71+
def addContentToFile(self, filePath, content):
72+
"""
73+
:type filePath: str
74+
:type content: str
75+
:rtype: None
76+
"""
77+
self.trie.addFileContentToPath(filePath, content)
78+
79+
def readContentFromFile(self, filePath):
80+
"""
81+
:type filePath: str
82+
:rtype: str
83+
"""
84+
return self.trie.readContentFromPath(filePath)
85+
86+
# Your FileSystem object will be instantiated and called as such:
87+
# obj = FileSystem()
88+
# param_1 = obj.ls(path)
89+
# obj.mkdir(path)
90+
# obj.addContentToFile(filePath,content)
91+
# param_4 = obj.readContentFromFile(filePath)

0 commit comments

Comments
 (0)