[LeetCode] Design In-Memory File System

588. Design In-Memory File System

Design a data structure that simulates an in-memory file system.

Implement the FileSystem class:

  • FileSystem() Initializes the object of the system.
  • List ls(String path)
  • If path is a file path, returns a list that only contains this file’s name.
  • If path is a directory path, returns the list of file and directory names in this directory.
  • The answer should in lexicographic order.
  • void mkdir(String path) Makes a new directory according to the given path. The given directory path does not exist. If the middle directories in the path do not exist, you should create them as well.
  • void addContentToFile(String filePath, String content)
  • If filePath does not exist, creates that file containing given content.
  • If filePath already exists, appends the given content to original content.
  • String readContentFromFile(String filePath) Returns the content in the file at filePath.
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
struct FileTree{
unordered_map<string, FileTree*> directory;
unordered_map<string, string> files;

FileTree(){};
FileTree* insertDir(string& p) {
if(!directory.count(p)) directory[p] = new FileTree();
return directory[p];
}
FileTree* getDir(string& p) {
return directory[p];
}
void insertFile(string& f, string& c) {
files[f] += c;
}
bool isDir(string& p) {
return directory.count(p);
}
bool isFile(string& p) {
return files.count(p);
}
bool hasFile() {
return !files.empty();
}
};
class FileSystem {
FileTree* ft;
string parse(string& s, int& i) {
string res = "";
while(i < s.length() and s[i] != '/') {
res += s[i++];
}
i++;
return res;
}
vector<string> _ls(FileTree* f) {
vector<string> res;

for(auto [k, _] : f->directory)
res.push_back(k);

for(auto [k, _] : f->files)
res.push_back(k);

sort(begin(res),end(res));

return res;
}
public:
FileSystem() {
ft = new FileTree();
}

vector<string> ls(string path) {
int i = 1;
FileTree* f = ft;
while(i < path.length()) {
auto p = parse(path,i);
if(i >= path.length()) {
if(f->isFile(p))
return {p};
else {
f = f->getDir(p);

return _ls(f);
}
} else {
f = f->getDir(p);
}
}
return _ls(f);
}

void mkdir(string path) {
int i = 1;
FileTree* f = ft;
while(i < path.length()) {
auto p = parse(path,i);
f = f->insertDir(p);
}
}

void addContentToFile(string filePath, string content) {
int i = 1;
FileTree* f = ft;
while(i < filePath.length()) {
auto p = parse(filePath,i);
if(i >= filePath.length()) {
f->insertFile(p,content);
} else {
f = f->insertDir(p);
}
}
}

string readContentFromFile(string filePath) {
int i = 1;
FileTree* f = ft;
while(i < filePath.length()) {
auto p = parse(filePath,i);
if(i >= filePath.length()) {
return f->files[p];
} else {
f = f->insertDir(p);
}
}
return "";
}
};

/**
* Your FileSystem object will be instantiated and called as such:
* FileSystem* obj = new FileSystem();
* vector<string> param_1 = obj->ls(path);
* obj->mkdir(path);
* obj->addContentToFile(filePath,content);
* string param_4 = obj->readContentFromFile(filePath);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/07/PS/LeetCode/design-in-memory-file-system/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.