From 3e67395f0b91510be80fd549bc76b1169bf61b28 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 19 Jul 2025 06:58:25 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1233 No.1233.Remove Sub-Folders from the Filesystem --- .../README.md | 242 ++++++++---------- .../README_EN.md | 242 ++++++++---------- .../Solution2.go | 51 ++-- .../Solution2.js | 66 +++-- .../Solution2.ts | 66 ++--- .../Solution3.go | 46 ---- 6 files changed, 301 insertions(+), 412 deletions(-) delete mode 100644 solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution3.go diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md index 2066a078751e3..0b2e18dccc984 100644 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md @@ -348,193 +348,151 @@ public: ```go type Trie struct { children map[string]*Trie - isEnd bool + fid int } func newTrie() *Trie { - m := map[string]*Trie{} - return &Trie{children: m} + return &Trie{map[string]*Trie{}, -1} } -func (this *Trie) insert(w string) { +func (this *Trie) insert(fid int, f string) { node := this - for _, p := range strings.Split(w, "/")[1:] { + ps := strings.Split(f, "/") + for _, p := range ps[1:] { if _, ok := node.children[p]; !ok { node.children[p] = newTrie() } - node, _ = node.children[p] + node = node.children[p] } - node.isEnd = true + node.fid = fid } -func (this *Trie) search(w string) bool { - node := this - for _, p := range strings.Split(w, "/")[1:] { - if _, ok := node.children[p]; !ok { - return false +func (this *Trie) search() (ans []int) { + var dfs func(*Trie) + dfs = func(root *Trie) { + if root.fid != -1 { + ans = append(ans, root.fid) + return } - node, _ = node.children[p] - if node.isEnd { - return true + for _, child := range root.children { + dfs(child) } } - return false + dfs(this) + return } -func removeSubfolders(folder []string) []string { - sort.Slice(folder, func(i, j int) bool { - return len(strings.Split(folder[i], "/")) < len(strings.Split(folder[j], "/")) - }) +func removeSubfolders(folder []string) (ans []string) { trie := newTrie() - var ans []string - for _, v := range folder { - if !trie.search(v) { - trie.insert(v) - ans = append(ans, v) - } + for i, f := range folder { + trie.insert(i, f) } - return ans + for _, i := range trie.search() { + ans = append(ans, folder[i]) + } + return } ``` #### TypeScript ```ts -function removeSubfolders(folder: string[]): string[] { - const createTrie = (): T => ({ '#': false, children: {} }); - const trie = createTrie(); +class Trie { + children: Record; + fid: number; - for (const f of folder) { - const path = f.split('/'); - path.shift(); + constructor() { + this.children = {}; + this.fid = -1; + } - let node = trie; - for (const p of path) { - if (!node.children[p]) node.children[p] = createTrie(); + insert(i: number, f: string): void { + let node: Trie = this; + const ps = f.split('/'); + for (let j = 1; j < ps.length; ++j) { + const p = ps[j]; + if (!(p in node.children)) { + node.children[p] = new Trie(); + } node = node.children[p]; } - node['#'] = true; + node.fid = i; } - const ans: string[] = []; - const dfs = (trie: T, path = '') => { - if (trie['#']) { - ans.push(path); - return; - } - - for (const key in trie.children) { - dfs(trie.children[key], path + '/' + key); - } - }; - - dfs(trie); - - return ans; + search(): number[] { + const ans: number[] = []; + const dfs = (root: Trie): void => { + if (root.fid !== -1) { + ans.push(root.fid); + return; + } + for (const child of Object.values(root.children)) { + dfs(child); + } + }; + dfs(this); + return ans; + } } -type T = { - '#': boolean; - children: Record; -}; +function removeSubfolders(folder: string[]): string[] { + const trie = new Trie(); + for (let i = 0; i < folder.length; ++i) { + trie.insert(i, folder[i]); + } + return trie.search().map(i => folder[i]); +} ``` #### JavaScript ```js -function removeSubfolders(folder) { - const createTrie = () => ({ '#': false, children: {} }); - const trie = createTrie(); - - for (const f of folder) { - const path = f.split('/'); - path.shift(); +class Trie { + constructor() { + this.children = {}; + this.fid = -1; + } - let node = trie; - for (const p of path) { - if (!node.children[p]) node.children[p] = createTrie(); + insert(i, f) { + let node = this; + const ps = f.split('/'); + for (let j = 1; j < ps.length; ++j) { + const p = ps[j]; + if (!(p in node.children)) { + node.children[p] = new Trie(); + } node = node.children[p]; } - node['#'] = true; + node.fid = i; } - const ans = []; - const dfs = (trie, path = '') => { - if (trie['#']) { - ans.push(path); - return; - } - - for (const key in trie.children) { - dfs(trie.children[key], path + '/' + key); - } - }; - - dfs(trie); - - return ans; -} -``` - - - - - - - -### 方法三 - - - -#### Go - -```go -type Trie struct { - children map[string]*Trie - fid int -} - -func newTrie() *Trie { - return &Trie{map[string]*Trie{}, -1} -} - -func (this *Trie) insert(fid int, f string) { - node := this - ps := strings.Split(f, "/") - for _, p := range ps[1:] { - if _, ok := node.children[p]; !ok { - node.children[p] = newTrie() - } - node = node.children[p] - } - node.fid = fid -} - -func (this *Trie) search() (ans []int) { - var dfs func(*Trie) - dfs = func(root *Trie) { - if root.fid != -1 { - ans = append(ans, root.fid) - return - } - for _, child := range root.children { - dfs(child) - } - } - dfs(this) - return + search() { + const ans = []; + const dfs = root => { + if (root.fid !== -1) { + ans.push(root.fid); + return; + } + for (const child of Object.values(root.children)) { + dfs(child); + } + }; + dfs(this); + return ans; + } } -func removeSubfolders(folder []string) (ans []string) { - trie := newTrie() - for i, f := range folder { - trie.insert(i, f) - } - for _, i := range trie.search() { - ans = append(ans, folder[i]) - } - return -} +/** + * @param {string[]} folder + * @return {string[]} + */ +var removeSubfolders = function (folder) { + const trie = new Trie(); + for (let i = 0; i < folder.length; ++i) { + trie.insert(i, folder[i]); + } + return trie.search().map(i => folder[i]); +}; ``` diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md index 6d9859eca427f..d4a4790b79abc 100644 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md @@ -347,193 +347,151 @@ public: ```go type Trie struct { children map[string]*Trie - isEnd bool + fid int } func newTrie() *Trie { - m := map[string]*Trie{} - return &Trie{children: m} + return &Trie{map[string]*Trie{}, -1} } -func (this *Trie) insert(w string) { +func (this *Trie) insert(fid int, f string) { node := this - for _, p := range strings.Split(w, "/")[1:] { + ps := strings.Split(f, "/") + for _, p := range ps[1:] { if _, ok := node.children[p]; !ok { node.children[p] = newTrie() } - node, _ = node.children[p] + node = node.children[p] } - node.isEnd = true + node.fid = fid } -func (this *Trie) search(w string) bool { - node := this - for _, p := range strings.Split(w, "/")[1:] { - if _, ok := node.children[p]; !ok { - return false +func (this *Trie) search() (ans []int) { + var dfs func(*Trie) + dfs = func(root *Trie) { + if root.fid != -1 { + ans = append(ans, root.fid) + return } - node, _ = node.children[p] - if node.isEnd { - return true + for _, child := range root.children { + dfs(child) } } - return false + dfs(this) + return } -func removeSubfolders(folder []string) []string { - sort.Slice(folder, func(i, j int) bool { - return len(strings.Split(folder[i], "/")) < len(strings.Split(folder[j], "/")) - }) +func removeSubfolders(folder []string) (ans []string) { trie := newTrie() - var ans []string - for _, v := range folder { - if !trie.search(v) { - trie.insert(v) - ans = append(ans, v) - } + for i, f := range folder { + trie.insert(i, f) } - return ans + for _, i := range trie.search() { + ans = append(ans, folder[i]) + } + return } ``` #### TypeScript ```ts -function removeSubfolders(folder: string[]): string[] { - const createTrie = (): T => ({ '#': false, children: {} }); - const trie = createTrie(); +class Trie { + children: Record; + fid: number; - for (const f of folder) { - const path = f.split('/'); - path.shift(); + constructor() { + this.children = {}; + this.fid = -1; + } - let node = trie; - for (const p of path) { - if (!node.children[p]) node.children[p] = createTrie(); + insert(i: number, f: string): void { + let node: Trie = this; + const ps = f.split('/'); + for (let j = 1; j < ps.length; ++j) { + const p = ps[j]; + if (!(p in node.children)) { + node.children[p] = new Trie(); + } node = node.children[p]; } - node['#'] = true; + node.fid = i; } - const ans: string[] = []; - const dfs = (trie: T, path = '') => { - if (trie['#']) { - ans.push(path); - return; - } - - for (const key in trie.children) { - dfs(trie.children[key], path + '/' + key); - } - }; - - dfs(trie); - - return ans; + search(): number[] { + const ans: number[] = []; + const dfs = (root: Trie): void => { + if (root.fid !== -1) { + ans.push(root.fid); + return; + } + for (const child of Object.values(root.children)) { + dfs(child); + } + }; + dfs(this); + return ans; + } } -type T = { - '#': boolean; - children: Record; -}; +function removeSubfolders(folder: string[]): string[] { + const trie = new Trie(); + for (let i = 0; i < folder.length; ++i) { + trie.insert(i, folder[i]); + } + return trie.search().map(i => folder[i]); +} ``` #### JavaScript ```js -function removeSubfolders(folder) { - const createTrie = () => ({ '#': false, children: {} }); - const trie = createTrie(); - - for (const f of folder) { - const path = f.split('/'); - path.shift(); +class Trie { + constructor() { + this.children = {}; + this.fid = -1; + } - let node = trie; - for (const p of path) { - if (!node.children[p]) node.children[p] = createTrie(); + insert(i, f) { + let node = this; + const ps = f.split('/'); + for (let j = 1; j < ps.length; ++j) { + const p = ps[j]; + if (!(p in node.children)) { + node.children[p] = new Trie(); + } node = node.children[p]; } - node['#'] = true; + node.fid = i; } - const ans = []; - const dfs = (trie, path = '') => { - if (trie['#']) { - ans.push(path); - return; - } - - for (const key in trie.children) { - dfs(trie.children[key], path + '/' + key); - } - }; - - dfs(trie); - - return ans; -} -``` - - - - - - - -### Solution 3 - - - -#### Go - -```go -type Trie struct { - children map[string]*Trie - fid int -} - -func newTrie() *Trie { - return &Trie{map[string]*Trie{}, -1} -} - -func (this *Trie) insert(fid int, f string) { - node := this - ps := strings.Split(f, "/") - for _, p := range ps[1:] { - if _, ok := node.children[p]; !ok { - node.children[p] = newTrie() - } - node = node.children[p] - } - node.fid = fid -} - -func (this *Trie) search() (ans []int) { - var dfs func(*Trie) - dfs = func(root *Trie) { - if root.fid != -1 { - ans = append(ans, root.fid) - return - } - for _, child := range root.children { - dfs(child) - } - } - dfs(this) - return + search() { + const ans = []; + const dfs = root => { + if (root.fid !== -1) { + ans.push(root.fid); + return; + } + for (const child of Object.values(root.children)) { + dfs(child); + } + }; + dfs(this); + return ans; + } } -func removeSubfolders(folder []string) (ans []string) { - trie := newTrie() - for i, f := range folder { - trie.insert(i, f) - } - for _, i := range trie.search() { - ans = append(ans, folder[i]) - } - return -} +/** + * @param {string[]} folder + * @return {string[]} + */ +var removeSubfolders = function (folder) { + const trie = new Trie(); + for (let i = 0; i < folder.length; ++i) { + trie.insert(i, folder[i]); + } + return trie.search().map(i => folder[i]); +}; ``` diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.go b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.go index 5f0a8f90351af..9a5f248168d9d 100644 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.go +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.go @@ -1,49 +1,46 @@ type Trie struct { children map[string]*Trie - isEnd bool + fid int } func newTrie() *Trie { - m := map[string]*Trie{} - return &Trie{children: m} + return &Trie{map[string]*Trie{}, -1} } -func (this *Trie) insert(w string) { +func (this *Trie) insert(fid int, f string) { node := this - for _, p := range strings.Split(w, "/")[1:] { + ps := strings.Split(f, "/") + for _, p := range ps[1:] { if _, ok := node.children[p]; !ok { node.children[p] = newTrie() } - node, _ = node.children[p] + node = node.children[p] } - node.isEnd = true + node.fid = fid } -func (this *Trie) search(w string) bool { - node := this - for _, p := range strings.Split(w, "/")[1:] { - if _, ok := node.children[p]; !ok { - return false +func (this *Trie) search() (ans []int) { + var dfs func(*Trie) + dfs = func(root *Trie) { + if root.fid != -1 { + ans = append(ans, root.fid) + return } - node, _ = node.children[p] - if node.isEnd { - return true + for _, child := range root.children { + dfs(child) } } - return false + dfs(this) + return } -func removeSubfolders(folder []string) []string { - sort.Slice(folder, func(i, j int) bool { - return len(strings.Split(folder[i], "/")) < len(strings.Split(folder[j], "/")) - }) +func removeSubfolders(folder []string) (ans []string) { trie := newTrie() - var ans []string - for _, v := range folder { - if !trie.search(v) { - trie.insert(v) - ans = append(ans, v) - } + for i, f := range folder { + trie.insert(i, f) + } + for _, i := range trie.search() { + ans = append(ans, folder[i]) } - return ans + return } \ No newline at end of file diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.js b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.js index 2c3e9e4bfca6e..4381e95c37f7f 100644 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.js +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.js @@ -1,32 +1,46 @@ -function removeSubfolders(folder) { - const createTrie = () => ({ '#': false, children: {} }); - const trie = createTrie(); - - for (const f of folder) { - const path = f.split('/'); - path.shift(); +class Trie { + constructor() { + this.children = {}; + this.fid = -1; + } - let node = trie; - for (const p of path) { - if (!node.children[p]) node.children[p] = createTrie(); + insert(i, f) { + let node = this; + const ps = f.split('/'); + for (let j = 1; j < ps.length; ++j) { + const p = ps[j]; + if (!(p in node.children)) { + node.children[p] = new Trie(); + } node = node.children[p]; } - node['#'] = true; + node.fid = i; } - const ans = []; - const dfs = (trie, path = '') => { - if (trie['#']) { - ans.push(path); - return; - } - - for (const key in trie.children) { - dfs(trie.children[key], path + '/' + key); - } - }; - - dfs(trie); - - return ans; + search() { + const ans = []; + const dfs = root => { + if (root.fid !== -1) { + ans.push(root.fid); + return; + } + for (const child of Object.values(root.children)) { + dfs(child); + } + }; + dfs(this); + return ans; + } } + +/** + * @param {string[]} folder + * @return {string[]} + */ +var removeSubfolders = function (folder) { + const trie = new Trie(); + for (let i = 0; i < folder.length; ++i) { + trie.insert(i, folder[i]); + } + return trie.search().map(i => folder[i]); +}; diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.ts b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.ts index 07802228b1dc7..fd6862e4b51a4 100644 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.ts +++ b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution2.ts @@ -1,37 +1,45 @@ -function removeSubfolders(folder: string[]): string[] { - const createTrie = (): T => ({ '#': false, children: {} }); - const trie = createTrie(); +class Trie { + children: Record; + fid: number; - for (const f of folder) { - const path = f.split('/'); - path.shift(); + constructor() { + this.children = {}; + this.fid = -1; + } - let node = trie; - for (const p of path) { - if (!node.children[p]) node.children[p] = createTrie(); + insert(i: number, f: string): void { + let node: Trie = this; + const ps = f.split('/'); + for (let j = 1; j < ps.length; ++j) { + const p = ps[j]; + if (!(p in node.children)) { + node.children[p] = new Trie(); + } node = node.children[p]; } - node['#'] = true; + node.fid = i; } - const ans: string[] = []; - const dfs = (trie: T, path = '') => { - if (trie['#']) { - ans.push(path); - return; - } - - for (const key in trie.children) { - dfs(trie.children[key], path + '/' + key); - } - }; - - dfs(trie); - - return ans; + search(): number[] { + const ans: number[] = []; + const dfs = (root: Trie): void => { + if (root.fid !== -1) { + ans.push(root.fid); + return; + } + for (const child of Object.values(root.children)) { + dfs(child); + } + }; + dfs(this); + return ans; + } } -type T = { - '#': boolean; - children: Record; -}; +function removeSubfolders(folder: string[]): string[] { + const trie = new Trie(); + for (let i = 0; i < folder.length; ++i) { + trie.insert(i, folder[i]); + } + return trie.search().map(i => folder[i]); +} diff --git a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution3.go b/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution3.go deleted file mode 100644 index 9a5f248168d9d..0000000000000 --- a/solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/Solution3.go +++ /dev/null @@ -1,46 +0,0 @@ -type Trie struct { - children map[string]*Trie - fid int -} - -func newTrie() *Trie { - return &Trie{map[string]*Trie{}, -1} -} - -func (this *Trie) insert(fid int, f string) { - node := this - ps := strings.Split(f, "/") - for _, p := range ps[1:] { - if _, ok := node.children[p]; !ok { - node.children[p] = newTrie() - } - node = node.children[p] - } - node.fid = fid -} - -func (this *Trie) search() (ans []int) { - var dfs func(*Trie) - dfs = func(root *Trie) { - if root.fid != -1 { - ans = append(ans, root.fid) - return - } - for _, child := range root.children { - dfs(child) - } - } - dfs(this) - return -} - -func removeSubfolders(folder []string) (ans []string) { - trie := newTrie() - for i, f := range folder { - trie.insert(i, f) - } - for _, i := range trie.search() { - ans = append(ans, folder[i]) - } - return -} \ No newline at end of file