-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign_file_system.py
122 lines (95 loc) · 3.65 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'''
You are asked to design a file system that allows you to create new paths and associate them with different values.
The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase
English letters. For example, "/leetcode" and "/leetcode/problems" are valid paths while an empty string "" and "/" are not.
Implement the FileSystem class:
bool createPath(string path, int value) Creates a new path and associates a value to it if possible and
returns true. Returns false if the path already exists or its parent path doesn't exist.
int get(string path) Returns the value associated with path or returns -1 if the path doesn't exist.
'''
from collections import defaultdict
class FileSystem(object):
def __init__(self):
self.path2value = defaultdict(int)
self.path2value[''] = -1
def createPath(self, path, value):
dirs = path.split('/')
parent = '/'.join(dirs[:-1])
if path in self.path2value or parent not in self.path2value:
return False
self.path2value[path] = value
return True
def get(self, path):
if path in self.path2value:
return self.path2value[path]
return -1
---------------------------
from collections import defaultdict
class TrieNode:
def __init__(self):
self.children = defaultdict(TrieNode)
self.isWord = False
self.value = 0
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, path, value):
cur_node = self.root
directories = path.split('/')[1:]
for d in directories[:-1]:
if d not in cur_node.children:
return False
cur_node = cur_node.children[d]
cur_node = cur_node.children[directories[-1]]
if not cur_node.isWord:
cur_node.isWord = True
cur_node.value = value
return True
return False
def search(self, path):
cur_node = self.root
directories = path.split('/')[1:]
for d in directories:
if d not in cur_node.children:
return -1
cur_node = cur_node.children[d]
if cur_node.isWord:
return cur_node.value
return -1
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)
-----------------------------
class TrieNode:
def __init__(self, value=0):
self.next = dict()
self.value = value
class FileSystem:
def __init__(self):
self.root = TrieNode()
def createPath(self, path: str, value: int) -> bool:
if not path or path == "/": # invalid path
return False
folders = path.split("/")[1:] # the first element is an empty string
node = self.root
for i, f in enumerate(folders):
if f not in node.next:
if i != len(folders) - 1: # the parent path doesn't exist
return False
# successfully create the new path
node.next[f] = TrieNode(value)
return True
node = node.next[f]
return False # the path already exists
def get(self, path: str) -> int:
folders = path.split("/")[1:]
node = self.root
for i, f in enumerate(folders):
if f not in node.next: # the path doesn't exist
return -1
node = node.next[f]
return node.value