1166. Design File System

1166. Design File System

Description

You are asked to design a file systemthat allows you to create new paths and associate them with different values.

The format of a path isone 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 emptystring "" and "/"are not.

Implement theFileSystem class:

  • bool createPath(string path, int value)Creates a new path and associates a value to it if possible and returns true.Returns falseif the path already exists or its parent path doesn’t exist .
  • int get(string path)Returns the value associated with path or returns-1if the path doesn’t exist.

Example 1:

1
2
3
4
5
6
7
8
9
10
Input:
["FileSystem","createPath","get"]
[[],["/a",1],["/a"]]
Output:
[null,true,1]
Explanation:
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/a", 1); // return true
fileSystem.get("/a"); // return 1

Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
["FileSystem","createPath","createPath","get","createPath","get"]
[[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]
Output:
[null,true,true,2,false,-1]
Explanation:
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/leet", 1); // return true
fileSystem.createPath("/leet/code", 2); // return true
fileSystem.get("/leet/code"); // return 2
fileSystem.createPath("/c/d", 1); // return false because the parent path "/c" doesn't exist.
fileSystem.get("/c"); // return -1 because this path doesn't exist.

Constraints:

  • 2 <= path.length <= 100
  • 1 <= value <= 10^9
  • Each path is valid and consists of lowercase English letters and '/'.
  • At most 10^4 calls in total will be made to createPath and get.

Hints/Notes

Solution

Language: C++

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
class FileSystem {
public:
struct TrieNode {
TrieNode(string name = ""): name(name) {}

string name;
int val = -1;
unordered_map<string, TrieNode*> m;
};

TrieNode* root;

FileSystem() {
root = new TrieNode();
}

bool createPath(string path, int value) {
vector<string> components = getComponents(path);
if (components.size() == 0) {
return false;
}
TrieNode* cur = root;
int n = components.size();
for (int i = 0; i < n - 1; i++) {
string& c = components[i];
if (!cur->m.contains(c)) {
return false;
}
cur = cur->m[c];
}
string new_path = components[n - 1];
if (cur->m.contains(new_path)) {
return false;
} else {
cur->m[new_path] = new TrieNode(new_path);
cur->m[new_path]->val = value;
return true;
}
}

int get(string path) {
vector<string> components = getComponents(path);
int n = components.size();
TrieNode* cur = root;
for (int i = 0; i < n; i++) {
string& c = components[i];
if (!cur->m.contains(c)) {
return -1;
}
cur = cur->m[c];
}
return cur->val;
}

vector<string> getComponents(string path) {
int start = 1, end;
vector<string> components;
while ((end = path.find('/', start)) != string::npos) {
components.push_back(path.substr(start, end - start));
start = end + 1;
}
components.push_back(path.substr(start));
return components;
}
};

/**
* Your FileSystem object will be instantiated and called as such:
* FileSystem* obj = new FileSystem();
* bool param_1 = obj->createPath(path,value);
* int param_2 = obj->get(path);
*/