-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1233-leetcode.cpp
97 lines (93 loc) · 2.44 KB
/
1233-leetcode.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<bits/stdc++.h>
using namespace std;
/**
* 1. Sorted the folders based on length
* 2. Constructed a trie for all the folders
*
*
*
*
**/
class Solution {
public:
class Folder {
public:
string name;
unordered_map<string,Folder*> childs;
bool empty;
Folder(string name){
this->name = name;
this->empty = false;
}
};
class Trie {
public:
Folder* root;
Trie(){
root = new Folder("/");
}
void add(vector<string>&s){
Folder* p = root;
for(int i=0;i<s.size();i++){
if(p->childs.find(s[i]) == p->childs.end()){
Folder* nf = new Folder(s[i]);
p->childs[s[i]] = nf;
p = p->childs[s[i]];
}else {
p = p->childs[s[i]];
}
}
p->empty = true;
}
void remove(vector<string> &s){
Folder *p = root;
for(int i=0;i<s.size();i++){
p = p->childs[s[i]];
}
p->childs.clear();
p->empty = true;
}
void dfs(Folder* root,string s,vector<string>&ans){
string str = root->name;
if(root->childs.size() == 0){
ans.push_back(s);
}
for(auto [c,f] : root->childs){
dfs(f,s +'/' + c,ans);
}
}
};
vector<string> getFolders(string &s){
vector<string>tokens;
string word= "";
for(int i=0;i<s.length();i++){
if(s[i] == '/'){
if(word.length()>0){
tokens.push_back(word);
word = "";
}
}else {
word+=s[i];
}
}
if(word.length()>0){
tokens.push_back(word);
word = "";
}
return tokens;
}
vector<string> removeSubfolders(vector<string>& folder) {
sort(folder.begin(),folder.end(),[&](const string &s1,const string &s2){
return s1.length()>s2.length();
});
Trie* trie = new Trie();
for(int i=0;i<folder.size();i++){
vector<string> f = getFolders(folder[i]);
trie->add(f);
trie->remove(f);
}
vector<string>ans;
trie->dfs(trie->root,"",ans);
return ans;
}
};