Skip to content

feat: add solutions to lc problem: No.1948 #4580

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 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bac33bb
Update README.md
samarthswami1016 Mar 11, 2025
4f2027d
Update README_EN.md
samarthswami1016 Mar 11, 2025
44776a5
Add files via upload
samarthswami1016 Mar 11, 2025
cecfdab
style: format code and docs with prettier
samarthswami1016 Mar 11, 2025
202e120
Update README.md
samarthswami1016 Mar 11, 2025
cb36982
Update README_EN.md
samarthswami1016 Mar 11, 2025
c26dddb
Merge branch 'main' into main
samarthswami1016 Mar 17, 2025
8637c02
Merge branch 'main' into main
samarthswami1016 Mar 20, 2025
5890b5c
Merge branch 'doocs:main' into main
samarthswami1016 Apr 4, 2025
0c9a8c9
Push
samarthswami1016 Apr 4, 2025
897d9ef
style: format code and docs with prettier
samarthswami1016 Apr 4, 2025
51e99d9
Merge branch 'doocs:main' into main
samarthswami1016 Apr 5, 2025
10d7d3a
Merge branch 'doocs:main' into main
samarthswami1016 Apr 8, 2025
8c56e29
feat: add solutions to lc problem: No.3500
samarthswami1016 Apr 8, 2025
09668b7
style: format code and docs with prettier
samarthswami1016 Apr 8, 2025
df3cc36
Merge branch 'doocs:main' into main
samarthswami1016 Apr 18, 2025
63cbebd
Merge branch 'doocs:main' into main
samarthswami1016 May 26, 2025
6ee2647
Merge branch 'doocs:main' into main
samarthswami1016 Jul 20, 2025
3b925b4
feat: add solutions to lc problem: No.1948
samarthswami1016 Jul 20, 2025
84ae946
Merge branch 'doocs:main' into main
samarthswami1016 Jul 20, 2025
78a3359
style: format code and docs with prettier
samarthswami1016 Jul 20, 2025
9862d73
feat: add solutions to lc problem: No.1948
samarthswami1016 Jul 20, 2025
c1281dc
Merge branch 'main' into main
samarthswami1016 Jul 20, 2025
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
498 changes: 341 additions & 157 deletions solution/1900-1999/1948.Delete Duplicate Folders in System/README.md

Large diffs are not rendered by default.

472 changes: 328 additions & 144 deletions solution/1900-1999/1948.Delete Duplicate Folders in System/README_EN.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
struct Trie {
string serial;
unordered_map<string, Trie*> children;
};

class Solution {
public:
vector<vector<string>> deleteDuplicateFolder(
vector<vector<string>>& paths) {
Trie* root = new Trie();

for (const vector<string>& path : paths) {
Trie* cur = root;
for (const string& node : path) {
if (!cur->children.count(node)) {
cur->children[node] = new Trie();
}
cur = cur->children[node];
}
}
unordered_map<string, int> freq;
function<void(Trie*)> construct = [&](Trie* node) {
if (node->children.empty()) {
return;
}

vector<string> v;
for (const auto& [folder, child] : node->children) {
construct(child);
v.push_back(folder + "(" + child->serial + ")");
}
sort(v.begin(), v.end());
for (string& s : v) {
node->serial += move(s);
}
++freq[node->serial];
};

construct(root);

vector<vector<string>> ans;
vector<string> path;

function<void(Trie*)> operate = [&](Trie* node) {
if (freq[node->serial] > 1) {
return;
}
if (!path.empty()) {
ans.push_back(path);
}
for (const auto& [folder, child] : node->children) {
path.push_back(folder);
operate(child);
path.pop_back();
}
};

operate(root);
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
type Trie struct {
serial string
children map[string]*Trie
}

func deleteDuplicateFolder(paths [][]string) [][]string {
root := &Trie{children: make(map[string]*Trie)}
for _, path := range paths {
cur := root
for _, node := range path {
if _, ok := cur.children[node]; !ok {
cur.children[node] = &Trie{children: make(map[string]*Trie)}
}
cur = cur.children[node]
}
}

freq := make(map[string]int)
var construct func(*Trie)
construct = func(node *Trie) {
if len(node.children) == 0 {
return
}
v := make([]string, 0, len(node.children))
for folder, child := range node.children {
construct(child)
v = append(v, folder+"("+child.serial+")")
}
sort.Strings(v)
node.serial = strings.Join(v, "")
freq[node.serial]++
}
construct(root)

ans := make([][]string, 0)
path := make([]string, 0)
var operate func(*Trie)
operate = func(node *Trie) {
if freq[node.serial] > 1 {
return
}
if len(path) > 0 {
tmp := make([]string, len(path))
copy(tmp, path)
ans = append(ans, tmp)
}
for folder, child := range node.children {
path = append(path, folder)
operate(child)
path = path[:len(path)-1]
}
}
operate(root)

return ans
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Solution {

class Trie {

String serial;
Map<String, Trie> children = new HashMap<>();
}

public List<List<String>> deleteDuplicateFolder(List<List<String>> paths) {
Trie root = new Trie();
for (List<String> path : paths) {
Trie cur = root;
for (String node : path) {
cur.children.putIfAbsent(node, new Trie());
cur = cur.children.get(node);
}
}

Map<String, Integer> freq = new HashMap<>();
construct(root, freq);
List<List<String>> ans = new ArrayList<>();
List<String> path = new ArrayList<>();
operate(root, freq, path, ans);
return ans;
}

private void construct(Trie node, Map<String, Integer> freq) {
if (node.children.isEmpty()) return;

List<String> v = new ArrayList<>();
for (Map.Entry<String, Trie> entry : node.children.entrySet()) {
construct(entry.getValue(), freq);
v.add(entry.getKey() + "(" + entry.getValue().serial + ")");
}

Collections.sort(v);
StringBuilder sb = new StringBuilder();
for (String s : v) {
sb.append(s);
}
node.serial = sb.toString();
freq.put(node.serial, freq.getOrDefault(node.serial, 0) + 1);
}

private void operate(
Trie node,
Map<String, Integer> freq,
List<String> path,
List<List<String>> ans
) {
if (freq.getOrDefault(node.serial, 0) > 1) return;

if (!path.isEmpty()) {
ans.add(new ArrayList<>(path));
}

for (Map.Entry<String, Trie> entry : node.children.entrySet()) {
path.add(entry.getKey());
operate(entry.getValue(), freq, path, ans);
path.remove(path.size() - 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var deleteDuplicateFolder = function (paths) {
class Trie {
constructor() {
this.serial = '';
this.children = new Map();
}
}

const root = new Trie();
for (const path of paths) {
let cur = root;
for (const node of path) {
if (!cur.children.has(node)) {
cur.children.set(node, new Trie());
}
cur = cur.children.get(node);
}
}

const freq = new Map();
function construct(node) {
if (node.children.size === 0) return;
const v = [];
for (const [folder, child] of node.children) {
construct(child);
v.push(`${folder}(${child.serial})`);
}
v.sort();
node.serial = v.join('');
freq.set(node.serial, (freq.get(node.serial) || 0) + 1);
}
construct(root);

const ans = [];
const path = [];
function operate(node) {
if ((freq.get(node.serial) || 0) > 1) return;
if (path.length > 0) {
ans.push([...path]);
}
for (const [folder, child] of node.children) {
path.push(folder);
operate(child);
path.pop();
}
}
operate(root);

return ans;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Trie:
serial: str = ""
children: dict

def __init__(self):
self.children = dict()


class Solution:
def deleteDuplicateFolder(self, paths: List[List[str]]) -> List[List[str]]:
root = Trie()
for path in paths:
cur = root
for node in path:
if node not in cur.children:
cur.children[node] = Trie()
cur = cur.children[node]
freq = Counter()
def construct(node: Trie) -> None:
if not node.children:
return
v = list()
for folder, child in node.children.items():
construct(child)
v.append(folder + "(" + child.serial + ")")
v.sort()
node.serial = "".join(v)
freq[node.serial] += 1
construct(root)
ans = list()
path = list()
def operate(node: Trie) -> None:
if freq[node.serial] > 1:
return
if path:
ans.append(path[:])
for folder, child in node.children.items():
path.append(folder)
operate(child)
path.pop()
operate(root)
return ans
Loading
Loading