diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/remove_subfolder_filesystem.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/remove_subfolder_filesystem.cpp')
-rw-r--r-- | other/remove_subfolder_filesystem.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/other/remove_subfolder_filesystem.cpp b/other/remove_subfolder_filesystem.cpp new file mode 100644 index 0000000..5ec1813 --- /dev/null +++ b/other/remove_subfolder_filesystem.cpp @@ -0,0 +1,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"); +} |