forked from tiationg-kho/leetcode-pattern-500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1166-design-file-system.py
52 lines (44 loc) · 1.34 KB
/
1166-design-file-system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class TrieNode:
def __init__(self):
self.children = {}
self.val = - 1
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, path, val):
path = path.split('/')[1:]
node = self.root
for i, p in enumerate(path):
if p not in node.children:
if i == len(path) - 1:
node.children[p] = TrieNode()
else:
return False
node = node.children[p]
if node.val == - 1:
node.val = val
return True
else:
return False
def search(self, path):
path = path.split('/')[1:]
node = self.root
for p in path:
if p not in node.children:
return - 1
node = node.children[p]
return node.val
class FileSystem:
def __init__(self):
self.trie = Trie()
def createPath(self, path: str, value: int) -> bool:
return self.trie.insert(path, value)
def get(self, path: str) -> int:
return self.trie.search(path)
# Your FileSystem object will be instantiated and called as such:
# obj = FileSystem()
# param_1 = obj.createPath(path,value)
# param_2 = obj.get(path)
# time O(1) for init, O(L) for createPath() and get()
# space O(nL)
# using trie and custom trie node