Skip to content

feat: add solutions to lc problem: No.1233 #4576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 100 additions & 142 deletions solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Trie>;
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<string, T>;
};
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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法三

<!-- tabs:start -->

#### 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]);
};
```

<!-- tabs:end -->
Expand Down
Loading