Skip to content

Commit c37ef9b

Browse files
authored
feat: add solutions to lc problem: No.1233 (#3670)
1 parent cf78167 commit c37ef9b

File tree

6 files changed

+271
-0
lines changed

6 files changed

+271
-0
lines changed

solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md

+97
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ func removeSubfolders(folder []string) []string {
155155
}
156156
```
157157

158+
#### TypeScript
159+
160+
```ts
161+
function removeSubfolders(folder: string[]): string[] {
162+
let s = folder[1];
163+
return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x));
164+
}
165+
```
166+
167+
#### JavaScript
168+
169+
```js
170+
function removeSubfolders(folder) {
171+
let s = folder[1];
172+
return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x));
173+
}
174+
```
175+
158176
<!-- tabs:end -->
159177

160178
<!-- solution:end -->
@@ -379,6 +397,85 @@ func removeSubfolders(folder []string) []string {
379397
}
380398
```
381399

400+
#### TypeScript
401+
402+
```ts
403+
function removeSubfolders(folder: string[]): string[] {
404+
const createTrie = (): T => ({ '#': false, children: {} });
405+
const trie = createTrie();
406+
407+
for (const f of folder) {
408+
const path = f.split('/');
409+
path.shift();
410+
411+
let node = trie;
412+
for (const p of path) {
413+
if (!node.children[p]) node.children[p] = createTrie();
414+
node = node.children[p];
415+
}
416+
node['#'] = true;
417+
}
418+
419+
const ans: string[] = [];
420+
const dfs = (trie: T, path = '') => {
421+
if (trie['#']) {
422+
ans.push(path);
423+
return;
424+
}
425+
426+
for (const key in trie.children) {
427+
dfs(trie.children[key], path + '/' + key);
428+
}
429+
};
430+
431+
dfs(trie);
432+
433+
return ans;
434+
}
435+
436+
type T = {
437+
'#': boolean;
438+
children: Record<string, T>;
439+
};
440+
```
441+
442+
#### JavaScript
443+
444+
```js
445+
function removeSubfolders(folder) {
446+
const createTrie = () => ({ '#': false, children: {} });
447+
const trie = createTrie();
448+
449+
for (const f of folder) {
450+
const path = f.split('/');
451+
path.shift();
452+
453+
let node = trie;
454+
for (const p of path) {
455+
if (!node.children[p]) node.children[p] = createTrie();
456+
node = node.children[p];
457+
}
458+
node['#'] = true;
459+
}
460+
461+
const ans = [];
462+
const dfs = (trie, path = '') => {
463+
if (trie['#']) {
464+
ans.push(path);
465+
return;
466+
}
467+
468+
for (const key in trie.children) {
469+
dfs(trie.children[key], path + '/' + key);
470+
}
471+
};
472+
473+
dfs(trie);
474+
475+
return ans;
476+
}
477+
```
478+
382479
<!-- tabs:end -->
383480

384481
<!-- solution:end -->

solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md

+97
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ func removeSubfolders(folder []string) []string {
154154
}
155155
```
156156

157+
#### TypeScript
158+
159+
```ts
160+
function removeSubfolders(folder: string[]): string[] {
161+
let s = folder[1];
162+
return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x));
163+
}
164+
```
165+
166+
#### JavaScript
167+
168+
```js
169+
function removeSubfolders(folder) {
170+
let s = folder[1];
171+
return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x));
172+
}
173+
```
174+
157175
<!-- tabs:end -->
158176

159177
<!-- solution:end -->
@@ -378,6 +396,85 @@ func removeSubfolders(folder []string) []string {
378396
}
379397
```
380398

399+
#### TypeScript
400+
401+
```ts
402+
function removeSubfolders(folder: string[]): string[] {
403+
const createTrie = (): T => ({ '#': false, children: {} });
404+
const trie = createTrie();
405+
406+
for (const f of folder) {
407+
const path = f.split('/');
408+
path.shift();
409+
410+
let node = trie;
411+
for (const p of path) {
412+
if (!node.children[p]) node.children[p] = createTrie();
413+
node = node.children[p];
414+
}
415+
node['#'] = true;
416+
}
417+
418+
const ans: string[] = [];
419+
const dfs = (trie: T, path = '') => {
420+
if (trie['#']) {
421+
ans.push(path);
422+
return;
423+
}
424+
425+
for (const key in trie.children) {
426+
dfs(trie.children[key], path + '/' + key);
427+
}
428+
};
429+
430+
dfs(trie);
431+
432+
return ans;
433+
}
434+
435+
type T = {
436+
'#': boolean;
437+
children: Record<string, T>;
438+
};
439+
```
440+
441+
#### JavaScript
442+
443+
```js
444+
function removeSubfolders(folder) {
445+
const createTrie = () => ({ '#': false, children: {} });
446+
const trie = createTrie();
447+
448+
for (const f of folder) {
449+
const path = f.split('/');
450+
path.shift();
451+
452+
let node = trie;
453+
for (const p of path) {
454+
if (!node.children[p]) node.children[p] = createTrie();
455+
node = node.children[p];
456+
}
457+
node['#'] = true;
458+
}
459+
460+
const ans = [];
461+
const dfs = (trie, path = '') => {
462+
if (trie['#']) {
463+
ans.push(path);
464+
return;
465+
}
466+
467+
for (const key in trie.children) {
468+
dfs(trie.children[key], path + '/' + key);
469+
}
470+
};
471+
472+
dfs(trie);
473+
474+
return ans;
475+
}
476+
```
477+
381478
<!-- tabs:end -->
382479

383480
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function removeSubfolders(folder) {
2+
let s = folder[1];
3+
return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x));
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function removeSubfolders(folder: string[]): string[] {
2+
let s = folder[1];
3+
return folder.sort().filter(x => !x.startsWith(s + '/') && (s = x));
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function removeSubfolders(folder) {
2+
const createTrie = () => ({ '#': false, children: {} });
3+
const trie = createTrie();
4+
5+
for (const f of folder) {
6+
const path = f.split('/');
7+
path.shift();
8+
9+
let node = trie;
10+
for (const p of path) {
11+
if (!node.children[p]) node.children[p] = createTrie();
12+
node = node.children[p];
13+
}
14+
node['#'] = true;
15+
}
16+
17+
const ans = [];
18+
const dfs = (trie, path = '') => {
19+
if (trie['#']) {
20+
ans.push(path);
21+
return;
22+
}
23+
24+
for (const key in trie.children) {
25+
dfs(trie.children[key], path + '/' + key);
26+
}
27+
};
28+
29+
dfs(trie);
30+
31+
return ans;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function removeSubfolders(folder: string[]): string[] {
2+
const createTrie = (): T => ({ '#': false, children: {} });
3+
const trie = createTrie();
4+
5+
for (const f of folder) {
6+
const path = f.split('/');
7+
path.shift();
8+
9+
let node = trie;
10+
for (const p of path) {
11+
if (!node.children[p]) node.children[p] = createTrie();
12+
node = node.children[p];
13+
}
14+
node['#'] = true;
15+
}
16+
17+
const ans: string[] = [];
18+
const dfs = (trie: T, path = '') => {
19+
if (trie['#']) {
20+
ans.push(path);
21+
return;
22+
}
23+
24+
for (const key in trie.children) {
25+
dfs(trie.children[key], path + '/' + key);
26+
}
27+
};
28+
29+
dfs(trie);
30+
31+
return ans;
32+
}
33+
34+
type T = {
35+
'#': boolean;
36+
children: Record<string, T>;
37+
};

0 commit comments

Comments
 (0)