From 073b2846d2f963e1f6ad7177a8822973d5386ed9 Mon Sep 17 00:00:00 2001 From: chayan das Date: Sat, 19 Jul 2025 12:57:13 +0530 Subject: [PATCH] Create 1233. Remove Sub-Folders from the Filesystem1 --- 1233. Remove Sub-Folders from the Filesystem1 | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1233. Remove Sub-Folders from the Filesystem1 diff --git a/1233. Remove Sub-Folders from the Filesystem1 b/1233. Remove Sub-Folders from the Filesystem1 new file mode 100644 index 0000000..0859fac --- /dev/null +++ b/1233. Remove Sub-Folders from the Filesystem1 @@ -0,0 +1,32 @@ +class Solution { +public: + vector removeSubfolders(vector& folder) { + // Sort all folders lexicographically + // This ensures parent folders come before their subfolders + sort(folder.begin(), folder.end()); + + vector result; + + // Add the first folder - it can't be a subfolder of anything + result.push_back(folder[0]); + + // Check each folder starting from the second one + for(int i = 1; i < folder.size(); i++) { + // Get the last folder we added to result + string lastFolder = result.back(); + + // Add "/" to ensure we're checking for actual subfolders + // This prevents "/a" from being considered a parent of "/ab" + lastFolder += "/"; + + // Check if current folder starts with the last added folder + // If it doesn't start with lastFolder, it's not a subfolder + if(folder[i].substr(0, lastFolder.length()) != lastFolder) { + result.push_back(folder[i]); + } + // If it does start with lastFolder, it's a subfolder, so skip it + } + + return result; + } +};