-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDelete Duplicate Folders in System.cpp
54 lines (46 loc) · 1.48 KB
/
Delete Duplicate Folders in System.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
struct Trie {
void insert(const vector<string>& path) {
auto node = this;
for (auto& p: path) {
if (!node->children.count(p)) {
node->children[p] = make_unique<Trie>();
}
node = node->children[p].get();
}
}
map<string, unique_ptr<Trie>> children;
};
class Solution {
Trie root;
map<string, Trie*> seen;
unordered_set<Trie*> toDelete;
string deduplicate(Trie* node) {
string path;
for (auto& [s, n]: node->children) {
path += s + deduplicate(n.get());
}
if (path.empty()) return "";
if (seen.count(path)) {
toDelete.insert(seen[path]);
toDelete.insert(node);
} else seen[path] = node;
return "(" + path + ")";
}
void getPaths(Trie* node, vector<vector<string>>& paths, vector<string>& path) {
for (auto& [s, n]: node->children) {
if (toDelete.count(n.get())) continue;
path.push_back(s);
getPaths(n.get(), paths, path);
path.pop_back();
}
if (!path.empty()) paths.push_back(path);
}
public:
vector<vector<string>> deleteDuplicateFolder(vector<vector<string>>& paths) {
for (auto& path: paths) root.insert(path);
paths.clear();
deduplicate(&root);
getPaths(&root, paths, vector<string>() = {});
return paths;
}
};