Skip to content

Commit 7ad1dbb

Browse files
committed
Add solution #588
1 parent 807a70d commit 7ad1dbb

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@
561561
582|[Kill Process](./solutions/0582-kill-process.js)|Medium|
562562
583|[Delete Operation for Two Strings](./solutions/0583-delete-operation-for-two-strings.js)|Medium|
563563
587|[Erect the Fence](./solutions/0587-erect-the-fence.js)|Hard|
564+
588|[Design In-Memory File System](./solutions/0588-design-in-memory-file-system.js)|Hard|
564565
589|[N-ary Tree Preorder Traversal](./solutions/0589-n-ary-tree-preorder-traversal.js)|Easy|
565566
590|[N-ary Tree Postorder Traversal](./solutions/0590-n-ary-tree-postorder-traversal.js)|Easy|
566567
591|[Tag Validator](./solutions/0591-tag-validator.js)|Hard|
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* 588. Design In-Memory File System
3+
* https://leetcode.com/problems/design-in-memory-file-system/
4+
* Difficulty: Hard
5+
*
6+
* Design a data structure that simulates an in-memory file system.
7+
*
8+
* Implement the FileSystem class:
9+
* - FileSystem() Initializes the object of the system.
10+
* - List<String> ls(String path)
11+
* - If path is a file path, returns a list that only contains this file's name.
12+
* - If path is a directory path, returns the list of file and directory names in this directory.
13+
* - The answer should in lexicographic order.
14+
* - void mkdir(String path) Makes a new directory according to the given path. The given directory
15+
* path does not exist. If the middle directories in the path do not exist, you should create
16+
* them as well.
17+
* - void addContentToFile(String filePath, String content)
18+
* - If filePath does not exist, creates that file containing given content.
19+
* - If filePath already exists, appends the given content to original content.
20+
* - String readContentFromFile(String filePath) Returns the content in the file at filePath.
21+
*/
22+
23+
var FileSystem = function() {
24+
this.root = new Map();
25+
};
26+
27+
/**
28+
* @param {string} path
29+
* @return {string[]}
30+
*/
31+
FileSystem.prototype.ls = function(path) {
32+
const parts = path === '/' ? [] : path.split('/').slice(1);
33+
let current = this.root;
34+
35+
for (const part of parts) {
36+
current = current.get(part);
37+
}
38+
39+
if (typeof current === 'string') {
40+
return [parts[parts.length - 1]];
41+
}
42+
43+
return Array.from(current.keys()).sort();
44+
};
45+
46+
/**
47+
* @param {string} path
48+
* @return {void}
49+
*/
50+
FileSystem.prototype.mkdir = function(path) {
51+
const parts = path.split('/').slice(1);
52+
let current = this.root;
53+
54+
for (const part of parts) {
55+
if (!current.has(part)) {
56+
current.set(part, new Map());
57+
}
58+
current = current.get(part);
59+
}
60+
};
61+
62+
/**
63+
* @param {string} filePath
64+
* @param {string} content
65+
* @return {void}
66+
*/
67+
FileSystem.prototype.addContentToFile = function(filePath, content) {
68+
const parts = filePath.split('/').slice(1);
69+
const fileName = parts.pop();
70+
let current = this.root;
71+
72+
for (const part of parts) {
73+
if (!current.has(part)) {
74+
current.set(part, new Map());
75+
}
76+
current = current.get(part);
77+
}
78+
79+
const existingContent = current.get(fileName) || '';
80+
current.set(fileName, existingContent + content);
81+
};
82+
83+
/**
84+
* @param {string} filePath
85+
* @return {string}
86+
*/
87+
FileSystem.prototype.readContentFromFile = function(filePath) {
88+
const parts = filePath.split('/').slice(1);
89+
const fileName = parts.pop();
90+
let current = this.root;
91+
92+
for (const part of parts) {
93+
current = current.get(part);
94+
}
95+
96+
return current.get(fileName);
97+
};

0 commit comments

Comments
 (0)