-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1166.py
41 lines (29 loc) · 1.01 KB
/
m1166.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
class FileSystem:
def __init__(self):
self.trie = {}
def createPath(self, path: str, value: int) -> bool:
pathChunks = path.split('/')[1:]
curr = self.trie
for i in range(len(pathChunks) - 1) :
if pathChunks[i] not in curr :
return False
curr = curr[pathChunks[i]]
if pathChunks[-1] in curr and 'value' in curr[pathChunks[-1]] :
return False
curr[pathChunks[-1]] = {}
curr[pathChunks[-1]]['value'] = value
return True
def get(self, path: str) -> int:
pathChunks = path.split('/')[1:]
curr = self.trie
for chunk in pathChunks :
if chunk not in curr :
return -1
curr = curr[chunk]
if 'value' in curr :
return curr['value']
return -1
# Your FileSystem object will be instantiated and called as such:
# obj = FileSystem()
# param_1 = obj.createPath(path,value)
# param_2 = obj.get(path)