-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveSubfolders.cpp
More file actions
91 lines (77 loc) · 2.58 KB
/
removeSubfolders.cpp
File metadata and controls
91 lines (77 loc) · 2.58 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Solution {
// Trie class
class Trie {
// Node class
class node{
public:
string folder;
unordered_map<string, node*> trie;
bool terminal = false;
};
// root pointer
node* root;
public:
Trie() {
root = new node();
}
// Utility function to split strings using a delimiter
vector<string> split(string text, char delim) {
string line;
vector<string> vec;
stringstream ss(text);
while (getline(ss, line, delim)) {
if (line != "")
vec.push_back(line);
}
return vec;
}
// Insert into the trie
void insert(string folder) {
node* temp = root;
auto folders = split(folder, '/');
for(auto folder : folders) {
// if it's already terminal, don't add anything more.
if(temp->terminal) return;
if(temp->trie[folder] == NULL){
node* new_node = new node();
temp->trie[folder] = new_node;
}
temp = temp->trie[folder];
}
// mark the terminal true at the end.
temp->terminal = true;
}
// Add folders to the result
// iterate over the entire trie root.
// if terminal true encounters, add that to the result vector.
void sumFolders(vector<string>& result, string folder, node* head) {
if(!head) head = root;
node* temp = head;
if(temp->terminal){
result.emplace_back(folder);
return;
}
auto mp = temp->trie;
for(auto itr : mp){
sumFolders(result, folder+"/"+itr.first, itr.second);
}
}
};
public:
vector<string> removeSubfolders(vector<string>& folders) {
// Instantiate a new trie
Trie* trie = new Trie();
// Insert each and every folder into the trie
// The trie is modified such that, if it detects a terminal true,
// it straight away returns without adding anything else.
for(string folder : folders){
trie->insert(folder);
}
// Create a new result vector
vector<string> result;
// Fill the vector
trie->sumFolders(result, "", nullptr);
// return result
return result;
}
};