Description
Submission
class Solution {
bool samePrefix(string s1, string s2) {
if(s2.length() > s1.length()) swap(s1, s2);
return s1.substr(0, s2.length()) == s2;
}
public:
vector<string> removeSubfolders(vector<string>& folder) {
int n = folder.size();
vector<int> remain(n, 1);
vector<string> rets;
for(auto& f: folder) {
f.push_back('/');
}
sort(folder.begin(), folder.end());
for(int i = 0; i < n; ++i) {
auto& s1 = folder[i];
int j = i + 1;
for(; j < n; ++j) {
auto& s2 = folder[j];
if(samePrefix(s1, s2)) remain[j] = 0;
else {
i = j - 1;
break;
}
}
}
for(int i = 0; i < n; ++i) {
if(remain[i]) {
folder[i].pop_back();
rets.push_back(folder[i]);
}
}
return rets;
}
};