forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
remove-sub-folders-from-the-filesystem.cpp
76 lines (69 loc) · 2.04 KB
/
remove-sub-folders-from-the-filesystem.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Time: O(n), n is the total sum of the lengths of folder names
// Space: O(t), t is the number of nodes in trie
class Solution {
public:
vector<string> removeSubfolders(vector<string>& folder) {
TrieNode trie;
for (const auto& s : folder) {
trie.Insert(split(s, '/'));
}
vector<string> result;
vector<string> path;
dfs(&trie, &path, &result);
return result;
}
private:
struct TrieNode {
bool is_end = false;
unordered_map<string, TrieNode *> leaves;
void Insert(const vector<string>& s) {
auto* p = this;
for (const auto& c : s) {
if (c.empty()) {
continue;
}
if (!p->leaves.count(c)) {
p->leaves[c] = new TrieNode;
}
p = p->leaves[c];
}
p->is_end = true;
}
~TrieNode() {
for (auto& kv : leaves) {
if (kv.second) {
delete kv.second;
}
}
}
};
void dfs(TrieNode *curr, vector<string> *path, vector<string> *result) {
if (curr->is_end) {
result->emplace_back(join(*path, '/'));
return;
}
for (const auto& kvp : curr->leaves) {
path->emplace_back(kvp.first);
dfs(kvp.second, path, result);
path->pop_back();
}
}
vector<string> split(const string& s, const char delim) {
vector<string> result;
auto end = string::npos;
do {
const auto& start = end + 1;
end = s.find(delim, start);
result.emplace_back(s.substr(start, end - start));
} while (end != string::npos);
return result;
}
string join(const vector<string>& names, const char delim) {
string result;
for (const auto& name : names) {
result.push_back('/');
result += name;
}
return result;
}
};