Skip to content

Commit 39c895b

Browse files
authored
feat: add solutions to lc problem: No.1233 (#4576)
No.1233.Remove Sub-Folders from the Filesystem
1 parent de776ba commit 39c895b

File tree

6 files changed

+301
-412
lines changed

6 files changed

+301
-412
lines changed

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

Lines changed: 100 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -348,193 +348,151 @@ public:
348348
```go
349349
type Trie struct {
350350
children map[string]*Trie
351-
isEnd bool
351+
fid int
352352
}
353353

354354
func newTrie() *Trie {
355-
m := map[string]*Trie{}
356-
return &Trie{children: m}
355+
return &Trie{map[string]*Trie{}, -1}
357356
}
358357

359-
func (this *Trie) insert(w string) {
358+
func (this *Trie) insert(fid int, f string) {
360359
node := this
361-
for _, p := range strings.Split(w, "/")[1:] {
360+
ps := strings.Split(f, "/")
361+
for _, p := range ps[1:] {
362362
if _, ok := node.children[p]; !ok {
363363
node.children[p] = newTrie()
364364
}
365-
node, _ = node.children[p]
365+
node = node.children[p]
366366
}
367-
node.isEnd = true
367+
node.fid = fid
368368
}
369369

370-
func (this *Trie) search(w string) bool {
371-
node := this
372-
for _, p := range strings.Split(w, "/")[1:] {
373-
if _, ok := node.children[p]; !ok {
374-
return false
370+
func (this *Trie) search() (ans []int) {
371+
var dfs func(*Trie)
372+
dfs = func(root *Trie) {
373+
if root.fid != -1 {
374+
ans = append(ans, root.fid)
375+
return
375376
}
376-
node, _ = node.children[p]
377-
if node.isEnd {
378-
return true
377+
for _, child := range root.children {
378+
dfs(child)
379379
}
380380
}
381-
return false
381+
dfs(this)
382+
return
382383
}
383384

384-
func removeSubfolders(folder []string) []string {
385-
sort.Slice(folder, func(i, j int) bool {
386-
return len(strings.Split(folder[i], "/")) < len(strings.Split(folder[j], "/"))
387-
})
385+
func removeSubfolders(folder []string) (ans []string) {
388386
trie := newTrie()
389-
var ans []string
390-
for _, v := range folder {
391-
if !trie.search(v) {
392-
trie.insert(v)
393-
ans = append(ans, v)
394-
}
387+
for i, f := range folder {
388+
trie.insert(i, f)
395389
}
396-
return ans
390+
for _, i := range trie.search() {
391+
ans = append(ans, folder[i])
392+
}
393+
return
397394
}
398395
```
399396

400397
#### TypeScript
401398

402399
```ts
403-
function removeSubfolders(folder: string[]): string[] {
404-
const createTrie = (): T => ({ '#': false, children: {} });
405-
const trie = createTrie();
400+
class Trie {
401+
children: Record<string, Trie>;
402+
fid: number;
406403

407-
for (const f of folder) {
408-
const path = f.split('/');
409-
path.shift();
404+
constructor() {
405+
this.children = {};
406+
this.fid = -1;
407+
}
410408

411-
let node = trie;
412-
for (const p of path) {
413-
if (!node.children[p]) node.children[p] = createTrie();
409+
insert(i: number, f: string): void {
410+
let node: Trie = this;
411+
const ps = f.split('/');
412+
for (let j = 1; j < ps.length; ++j) {
413+
const p = ps[j];
414+
if (!(p in node.children)) {
415+
node.children[p] = new Trie();
416+
}
414417
node = node.children[p];
415418
}
416-
node['#'] = true;
419+
node.fid = i;
417420
}
418421

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;
422+
search(): number[] {
423+
const ans: number[] = [];
424+
const dfs = (root: Trie): void => {
425+
if (root.fid !== -1) {
426+
ans.push(root.fid);
427+
return;
428+
}
429+
for (const child of Object.values(root.children)) {
430+
dfs(child);
431+
}
432+
};
433+
dfs(this);
434+
return ans;
435+
}
434436
}
435437

436-
type T = {
437-
'#': boolean;
438-
children: Record<string, T>;
439-
};
438+
function removeSubfolders(folder: string[]): string[] {
439+
const trie = new Trie();
440+
for (let i = 0; i < folder.length; ++i) {
441+
trie.insert(i, folder[i]);
442+
}
443+
return trie.search().map(i => folder[i]);
444+
}
440445
```
441446

442447
#### JavaScript
443448

444449
```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();
450+
class Trie {
451+
constructor() {
452+
this.children = {};
453+
this.fid = -1;
454+
}
452455

453-
let node = trie;
454-
for (const p of path) {
455-
if (!node.children[p]) node.children[p] = createTrie();
456+
insert(i, f) {
457+
let node = this;
458+
const ps = f.split('/');
459+
for (let j = 1; j < ps.length; ++j) {
460+
const p = ps[j];
461+
if (!(p in node.children)) {
462+
node.children[p] = new Trie();
463+
}
456464
node = node.children[p];
457465
}
458-
node['#'] = true;
466+
node.fid = i;
459467
}
460468

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-
479-
<!-- tabs:end -->
480-
481-
<!-- solution:end -->
482-
483-
<!-- solution:start -->
484-
485-
### 方法三
486-
487-
<!-- tabs:start -->
488-
489-
#### Go
490-
491-
```go
492-
type Trie struct {
493-
children map[string]*Trie
494-
fid int
495-
}
496-
497-
func newTrie() *Trie {
498-
return &Trie{map[string]*Trie{}, -1}
499-
}
500-
501-
func (this *Trie) insert(fid int, f string) {
502-
node := this
503-
ps := strings.Split(f, "/")
504-
for _, p := range ps[1:] {
505-
if _, ok := node.children[p]; !ok {
506-
node.children[p] = newTrie()
507-
}
508-
node = node.children[p]
509-
}
510-
node.fid = fid
511-
}
512-
513-
func (this *Trie) search() (ans []int) {
514-
var dfs func(*Trie)
515-
dfs = func(root *Trie) {
516-
if root.fid != -1 {
517-
ans = append(ans, root.fid)
518-
return
519-
}
520-
for _, child := range root.children {
521-
dfs(child)
522-
}
523-
}
524-
dfs(this)
525-
return
469+
search() {
470+
const ans = [];
471+
const dfs = root => {
472+
if (root.fid !== -1) {
473+
ans.push(root.fid);
474+
return;
475+
}
476+
for (const child of Object.values(root.children)) {
477+
dfs(child);
478+
}
479+
};
480+
dfs(this);
481+
return ans;
482+
}
526483
}
527484

528-
func removeSubfolders(folder []string) (ans []string) {
529-
trie := newTrie()
530-
for i, f := range folder {
531-
trie.insert(i, f)
532-
}
533-
for _, i := range trie.search() {
534-
ans = append(ans, folder[i])
535-
}
536-
return
537-
}
485+
/**
486+
* @param {string[]} folder
487+
* @return {string[]}
488+
*/
489+
var removeSubfolders = function (folder) {
490+
const trie = new Trie();
491+
for (let i = 0; i < folder.length; ++i) {
492+
trie.insert(i, folder[i]);
493+
}
494+
return trie.search().map(i => folder[i]);
495+
};
538496
```
539497

540498
<!-- tabs:end -->

0 commit comments

Comments
 (0)