-
Notifications
You must be signed in to change notification settings - Fork 3
/
1233.cpp
40 lines (40 loc) · 1.12 KB
/
1233.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
class Solution {
public:
vector<string> fold2vec(string& s) {
vector<string> res;
string sub = s.substr(1);
stringstream ss(sub);
string token;
while (getline(ss, token, '/')) {
res.push_back(token);
}
return res;
}
vector<string> removeSubfolders(vector<string>& folder) {
vector<string> res;
sort(folder.begin(), folder.end());
vector<string> prev;
for (auto& fold : folder) {
if (res.empty()) {
res.push_back(fold);
prev = fold2vec(fold);
}
else {
vector<string> curr = fold2vec(fold);
int n = min(prev.size(), curr.size());
bool diff = false;
for (int i = 0; i < n; ++i) {
if (prev[i] != curr[i]) {
diff = true;
break;
}
}
if (diff) {
res.push_back(fold);
prev = curr;
}
}
}
return res;
}
};