Skip to content

Commit 25406f4

Browse files
authored
Create 1233. Remove Sub-Folders from the Filesystem1 (#844)
2 parents ae505cc + 073b284 commit 25406f4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<string> removeSubfolders(vector<string>& folder) {
4+
// Sort all folders lexicographically
5+
// This ensures parent folders come before their subfolders
6+
sort(folder.begin(), folder.end());
7+
8+
vector<string> result;
9+
10+
// Add the first folder - it can't be a subfolder of anything
11+
result.push_back(folder[0]);
12+
13+
// Check each folder starting from the second one
14+
for(int i = 1; i < folder.size(); i++) {
15+
// Get the last folder we added to result
16+
string lastFolder = result.back();
17+
18+
// Add "/" to ensure we're checking for actual subfolders
19+
// This prevents "/a" from being considered a parent of "/ab"
20+
lastFolder += "/";
21+
22+
// Check if current folder starts with the last added folder
23+
// If it doesn't start with lastFolder, it's not a subfolder
24+
if(folder[i].substr(0, lastFolder.length()) != lastFolder) {
25+
result.push_back(folder[i]);
26+
}
27+
// If it does start with lastFolder, it's a subfolder, so skip it
28+
}
29+
30+
return result;
31+
}
32+
};

0 commit comments

Comments
 (0)