Skip to content

feat: add solution for deleting duplicate folders in a file system #4581

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 2 commits into from
Jul 20, 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
123 changes: 122 additions & 1 deletion solution/1900-1999/1948.Delete Duplicate Folders in System/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,58 @@ tags:
#### Python3

```python

class TrieNode:
def __init__(self):
self.children = collections.defaultdict(TrieNode)
self.key = ""
self.deleted = False


class Solution:
def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:
root = TrieNode()

for path in paths:
current = root
for folder in path:
current = current.children[folder]
current.key = folder

seen = collections.defaultdict(list)

def dfs(node):
if not node.children:
return ""
keys = []
for key, child in node.children.items():
serialized = dfs(child)
keys.append(f"{key}({serialized})")
keys.sort()
serialized = "".join(keys)
if len(seen[serialized]) > 0:
for duplicate in seen[serialized]:
duplicate.deleted = True
node.deleted = True
seen[serialized].append(node)
return serialized

dfs(root)

result = []
path = []

def collect(node):
if node.deleted:
return
if path:
result.append(path.copy())
for key, child in node.children.items():
path.append(key)
collect(child)
path.pop()

collect(root)
return result
```

#### Java
Expand All @@ -147,6 +198,76 @@ tags:
#### Go

```go
type TrieNode struct {
children map[string]*TrieNode
key string
deleted bool
}

func deleteDuplicateFolder(paths [][]string) [][]string {
root := &TrieNode{children: make(map[string]*TrieNode)}

for _, path := range paths {
current := root
for _, folder := range path {
if _, ok := current.children[folder]; !ok {
current.children[folder] = &TrieNode{
children: make(map[string]*TrieNode),
key: folder,
}
}
current = current.children[folder]
}
}

seen := make(map[string]*TrieNode)
var dfs func(*TrieNode) string
dfs = func(node *TrieNode) string {
if node == nil || len(node.children) == 0 {
return ""
}

var keys []string
for key, child := range node.children {
serialized := dfs(child)
keys = append(keys, key+"("+serialized+")")
}
sort.Strings(keys)
serialized := strings.Join(keys, "")

if existing, ok := seen[serialized]; ok {
existing.deleted = true
node.deleted = true
} else {
seen[serialized] = node
}

return serialized
}
dfs(root)

var result [][]string
var path []string
var collect func(*TrieNode)
collect = func(node *TrieNode) {
if node.deleted {
return
}
if len(path) > 0 {
newPath := make([]string, len(path))
copy(newPath, path)
result = append(result, newPath)
}
for key, child := range node.children {
path = append(path, key)
collect(child)
path = path[:len(path)-1]
}
}
collect(root)

return result
}

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ folder named "b".
<pre>
<strong>Input:</strong> paths = [[&quot;a&quot;],[&quot;c&quot;],[&quot;a&quot;,&quot;b&quot;],[&quot;c&quot;,&quot;b&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;x&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;x&quot;,&quot;y&quot;],[&quot;w&quot;],[&quot;w&quot;,&quot;y&quot;]]
<strong>Output:</strong> [[&quot;c&quot;],[&quot;c&quot;,&quot;b&quot;],[&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;]]
<strong>Explanation: </strong>The file structure is as shown.
<strong>Explanation: </strong>The file structure is as shown.
Folders &quot;/a/b/x&quot; and &quot;/w&quot; (and their subfolders) are marked for deletion because they both contain an empty folder named &quot;y&quot;.
Note that folders &quot;/a&quot; and &quot;/c&quot; are identical after the deletion, but they are not deleted because they were not marked beforehand.
</pre>
Expand Down Expand Up @@ -108,7 +108,58 @@ Note that the returned array can be in a different order as the order does not m
#### Python3

```python

class TrieNode:
def __init__(self):
self.children = collections.defaultdict(TrieNode)
self.key = ""
self.deleted = False


class Solution:
def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:
root = TrieNode()

for path in paths:
current = root
for folder in path:
current = current.children[folder]
current.key = folder

seen = collections.defaultdict(list)

def dfs(node):
if not node.children:
return ""
keys = []
for key, child in node.children.items():
serialized = dfs(child)
keys.append(f"{key}({serialized})")
keys.sort()
serialized = "".join(keys)
if len(seen[serialized]) > 0:
for duplicate in seen[serialized]:
duplicate.deleted = True
node.deleted = True
seen[serialized].append(node)
return serialized

dfs(root)

result = []
path = []

def collect(node):
if node.deleted:
return
if path:
result.append(path.copy())
for key, child in node.children.items():
path.append(key)
collect(child)
path.pop()

collect(root)
return result
```

#### Java
Expand All @@ -126,6 +177,76 @@ Note that the returned array can be in a different order as the order does not m
#### Go

```go
type TrieNode struct {
children map[string]*TrieNode
key string
deleted bool
}

func deleteDuplicateFolder(paths [][]string) [][]string {
root := &TrieNode{children: make(map[string]*TrieNode)}

for _, path := range paths {
current := root
for _, folder := range path {
if _, ok := current.children[folder]; !ok {
current.children[folder] = &TrieNode{
children: make(map[string]*TrieNode),
key: folder,
}
}
current = current.children[folder]
}
}

seen := make(map[string]*TrieNode)
var dfs func(*TrieNode) string
dfs = func(node *TrieNode) string {
if node == nil || len(node.children) == 0 {
return ""
}

var keys []string
for key, child := range node.children {
serialized := dfs(child)
keys = append(keys, key+"("+serialized+")")
}
sort.Strings(keys)
serialized := strings.Join(keys, "")

if existing, ok := seen[serialized]; ok {
existing.deleted = true
node.deleted = true
} else {
seen[serialized] = node
}

return serialized
}
dfs(root)

var result [][]string
var path []string
var collect func(*TrieNode)
collect = func(node *TrieNode) {
if node.deleted {
return
}
if len(path) > 0 {
newPath := make([]string, len(path))
copy(newPath, path)
result = append(result, newPath)
}
for key, child := range node.children {
path = append(path, key)
collect(child)
path = path[:len(path)-1]
}
}
collect(root)

return result
}

```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
type TrieNode struct {
children map[string]*TrieNode
key string
deleted bool
}

func deleteDuplicateFolder(paths [][]string) [][]string {
root := &TrieNode{children: make(map[string]*TrieNode)}

for _, path := range paths {
current := root
for _, folder := range path {
if _, ok := current.children[folder]; !ok {
current.children[folder] = &TrieNode{
children: make(map[string]*TrieNode),
key: folder,
}
}
current = current.children[folder]
}
}

seen := make(map[string]*TrieNode)
var dfs func(*TrieNode) string
dfs = func(node *TrieNode) string {
if node == nil || len(node.children) == 0 {
return ""
}

var keys []string
for key, child := range node.children {
serialized := dfs(child)
keys = append(keys, key+"("+serialized+")")
}
sort.Strings(keys)
serialized := strings.Join(keys, "")

if existing, ok := seen[serialized]; ok {
existing.deleted = true
node.deleted = true
} else {
seen[serialized] = node
}

return serialized
}
dfs(root)

var result [][]string
var path []string
var collect func(*TrieNode)
collect = func(node *TrieNode) {
if node.deleted {
return
}
if len(path) > 0 {
newPath := make([]string, len(path))
copy(newPath, path)
result = append(result, newPath)
}
for key, child := range node.children {
path = append(path, key)
collect(child)
path = path[:len(path)-1]
}
}
collect(root)

return result
}
Loading