Skip to content

1948. Delete Duplicate Folders in System solution #4578

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

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,75 @@ tags:
#### Java

```java
class Solution {
static class TrieNode {
Map<String, TrieNode> children = new HashMap<>();
String name;
boolean isDeleted = false;

TrieNode(String name) {
this.name = name;
}
}

Map<String, List<TrieNode>> serialMap = new HashMap<>();

public List<List<String>> deleteDuplicateFolder(List<List<String>> paths) {
TrieNode root = new TrieNode("");

// Step 1: Build the folder trie
for (List<String> path : paths) {
TrieNode curr = root;
for (String folder : path) {
curr.children.putIfAbsent(folder, new TrieNode(folder));
curr = curr.children.get(folder);
}
}

// Step 2: Serialize and collect duplicates
serialize(root);

// Step 3: Mark duplicate folders
for (List<TrieNode> group : serialMap.values()) {
if (group.size() > 1) {
for (TrieNode node : group) {
node.isDeleted = true;
}
}
}

// Step 4: Collect remaining paths
List<List<String>> result = new ArrayList<>();
dfs(root, new ArrayList<>(), result);
return result;
}

private String serialize(TrieNode node) {
if (node.children.isEmpty()) return "";

List<String> parts = new ArrayList<>();
for (String childName : node.children.keySet().stream().sorted().toList()) {
TrieNode child = node.children.get(childName);
String sub = serialize(child);
parts.add(childName + "[" + sub + "]");
}

String serial = String.join("", parts);
serialMap.computeIfAbsent(serial, k -> new ArrayList<>()).add(node);
return serial;
}

private void dfs(TrieNode node, List<String> path, List<List<String>> result) {
for (TrieNode child : node.children.values()) {
if (!child.isDeleted) {
path.add(child.name);
result.add(new ArrayList<>(path));
dfs(child, path, result);
path.remove(path.size() - 1);
}
}
}
}

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,75 @@ Note that the returned array can be in a different order as the order does not m
#### Java

```java
class Solution {
static class TrieNode {
Map<String, TrieNode> children = new HashMap<>();
String name;
boolean isDeleted = false;

TrieNode(String name) {
this.name = name;
}
}

Map<String, List<TrieNode>> serialMap = new HashMap<>();

public List<List<String>> deleteDuplicateFolder(List<List<String>> paths) {
TrieNode root = new TrieNode("");

// Step 1: Build the folder trie
for (List<String> path : paths) {
TrieNode curr = root;
for (String folder : path) {
curr.children.putIfAbsent(folder, new TrieNode(folder));
curr = curr.children.get(folder);
}
}

// Step 2: Serialize and collect duplicates
serialize(root);

// Step 3: Mark duplicate folders
for (List<TrieNode> group : serialMap.values()) {
if (group.size() > 1) {
for (TrieNode node : group) {
node.isDeleted = true;
}
}
}

// Step 4: Collect remaining paths
List<List<String>> result = new ArrayList<>();
dfs(root, new ArrayList<>(), result);
return result;
}

private String serialize(TrieNode node) {
if (node.children.isEmpty()) return "";

List<String> parts = new ArrayList<>();
for (String childName : node.children.keySet().stream().sorted().toList()) {
TrieNode child = node.children.get(childName);
String sub = serialize(child);
parts.add(childName + "[" + sub + "]");
}

String serial = String.join("", parts);
serialMap.computeIfAbsent(serial, k -> new ArrayList<>()).add(node);
return serial;
}

private void dfs(TrieNode node, List<String> path, List<List<String>> result) {
for (TrieNode child : node.children.values()) {
if (!child.isDeleted) {
path.add(child.name);
result.add(new ArrayList<>(path));
dfs(child, path, result);
path.remove(path.size() - 1);
}
}
}
}

```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Solution {
static class TrieNode {
Map<String, TrieNode> children = new HashMap<>();
String name;
boolean isDeleted = false;

TrieNode(String name) {
this.name = name;
}
}

Map<String, List<TrieNode>> serialMap = new HashMap<>();

public List<List<String>> deleteDuplicateFolder(List<List<String>> paths) {
TrieNode root = new TrieNode("");

// Step 1: Build the folder trie
for (List<String> path : paths) {
TrieNode curr = root;
for (String folder : path) {
curr.children.putIfAbsent(folder, new TrieNode(folder));
curr = curr.children.get(folder);
}
}

// Step 2: Serialize and collect duplicates
serialize(root);

// Step 3: Mark duplicate folders
for (List<TrieNode> group : serialMap.values()) {
if (group.size() > 1) {
for (TrieNode node : group) {
node.isDeleted = true;
}
}
}

// Step 4: Collect remaining paths
List<List<String>> result = new ArrayList<>();
dfs(root, new ArrayList<>(), result);
return result;
}

private String serialize(TrieNode node) {
if (node.children.isEmpty()) return "";

List<String> parts = new ArrayList<>();
for (String childName : node.children.keySet().stream().sorted().toList()) {
TrieNode child = node.children.get(childName);
String sub = serialize(child);
parts.add(childName + "[" + sub + "]");
}

String serial = String.join("", parts);
serialMap.computeIfAbsent(serial, k -> new ArrayList<>()).add(node);
return serial;
}

private void dfs(TrieNode node, List<String> path, List<List<String>> result) {
for (TrieNode child : node.children.values()) {
if (!child.isDeleted) {
path.add(child.name);
result.add(new ArrayList<>(path));
dfs(child, path, result);
path.remove(path.size() - 1);
}
}
}
}