-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1233. Remove Sub-Folders from the Filesystem.cpp
More file actions
51 lines (42 loc) · 1.23 KB
/
1233. Remove Sub-Folders from the Filesystem.cpp
File metadata and controls
51 lines (42 loc) · 1.23 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
class Solution {
public:
vector<string> removeSubfolders(vector<string>& folder) {
vector<string>ans;
sort(folder.begin(),folder.end());
ans.push_back(folder[0]);
for(int i = 1; i < folder.size(); i++) {
string curr = folder[i];
string last = ans.back();
last += '/';
//if curr folder is sustring of last folder and start at idx 0
if(curr.find(last) != 0){
ans.push_back(folder[i]);
}
}
return ans;
}
};
class Solution {
public:
vector<string> removeSubfolders(vector<string>& folder) {
vector<string>ans;
unordered_set<string>st;
for(auto temp : folder){
st.insert(temp);
}
for(string& str : folder){
int flag = 1;
string temp1 = str;
while(!str.empty()){
auto idx = str.find_last_of('/');
string temp = str.substr(0,idx);
if(st.find(temp) != st.end()){
flag = 0;
break;
}
}
if(flag == 1)ans.push_back(temp1);
}
return ans;
}
};