diff options
-rw-r--r-- | misc.cpp | 20 | ||||
-rw-r--r-- | remove_subfolder_filesystem.cpp | 48 | ||||
-rw-r--r-- | remove_subfolder_filesystem.py | 34 |
3 files changed, 102 insertions, 0 deletions
diff --git a/misc.cpp b/misc.cpp new file mode 100644 index 0000000..8263244 --- /dev/null +++ b/misc.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector<string> removeSubfolders(vector<string> &folder) { + sort(folder.begin(), folder.end()); + vector<string> res; + + for (auto s : folder) { + if (res.size() == 0) { + res.push_back(s); + continue; + } + + string parent = res[res.size() - 1]; + if (s.substr(0, parent.size() + 1) != parent + '/') + res.push_back(s); + } + + return res; + } +}; diff --git a/remove_subfolder_filesystem.cpp b/remove_subfolder_filesystem.cpp new file mode 100644 index 0000000..5ec1813 --- /dev/null +++ b/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"); +} diff --git a/remove_subfolder_filesystem.py b/remove_subfolder_filesystem.py new file mode 100644 index 0000000..484831f --- /dev/null +++ b/remove_subfolder_filesystem.py @@ -0,0 +1,34 @@ +# 1233. Remove Sub-Folders from the Filesystem + +""" +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(object): + def removeSubfolders(self, folder): + """ + :type folder: List[str] + :rtype: List[str] + """ + folder.sort(key=lambda x: len(x)) + vis = set() + for i in folder: + for j in range(2, len(i)): + if i[j] == "/" and i[:j] in vis: + break + else: + vis.add(i) + return list(vis) + + +if __name__ == "__main__": + obj = Solution() + print(obj.removeSubfolders(folder=["/a", "/a/b", "/c/d", "/c/d/e", "/c/f"])) + print(obj.removeSubfolders(folder=["/a", "/a/b/c", "/a/b/d"])) + print(obj.removeSubfolders(folder=["/a/b/c", "/a/b/ca", "/a/b/d"])) |