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
|
// 1233. Remove Sub-Folders from the Filesystem
#include "leetcode.h"
/*
* given a list of folders 'folder', return the folders after removing all the
* sub-folders in those folders. you may return the answer in any order. if a
* 'folder[i]' is located within another 'folder[j]', it is called a sub-folder
* of it. a sub-folder of 'folder[j]' must start with 'folder[j]', followed by
* '/'. the format of a path is one ormore concatenated strings of the form: '/'
* followed by one or more lowercase english letters.
*/
class Solution {
public:
vector<string> removeSubfolders(vector<string> &folder) {
sort(folder.begin(), folder.end());
vector<string> ans;
for (auto i : folder) {
if (!ans.size()) {
ans.push_back(i);
continue;
}
string parent = ans[ans.size() - 1];
if (i.substr(0, parent.size() + 1) != parent + '/')
ans.push_back(i);
}
return ans;
}
};
int main() {
Solution obj;
vector<string> f1 = {"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"},
rsf1 = obj.removeSubfolders(f1);
vector<string> f2 = {"/a", "/a/b/c", "/a/b/d"},
rsf2 = obj.removeSubfolders(f2);
vector<string> f3 = {"/a/b/c", "/a/b/ca", "/a/b/d"},
rsf3 = obj.removeSubfolders(f3);
for (int i = 0; i < rsf1.size(); i++)
cout << rsf1[i] << ' '; // expect: ["/a","/c/d","/c/f"]
printf("\n");
for (int i = 0; i < rsf2.size(); i++)
cout << rsf2[i] << ' '; // expect: ["/a"]
printf("\n");
for (int i = 0; i < rsf3.size(); i++)
cout << rsf3[i] << ' '; // expect: ["/a/b/c","/a/b/ca","/a/b/d"]
printf("\n");
}
|