{"id":5776,"date":"2019-10-21T22:38:07","date_gmt":"2019-10-22T05:38:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5776"},"modified":"2019-10-21T22:38:14","modified_gmt":"2019-10-22T05:38:14","slug":"leetcode-1233-remove-sub-folders-from-the-filesystem","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1233-remove-sub-folders-from-the-filesystem\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1233. Remove Sub-Folders from the Filesystem"},"content":{"rendered":"\n<p>Given a list of folders, remove all sub-folders in those folders and return in&nbsp;<strong>any order<\/strong>&nbsp;the folders after removing.<\/p>\n\n\n\n<p>If a&nbsp;<code>folder[i]<\/code>&nbsp;is located within&nbsp;another&nbsp;<code>folder[j]<\/code>, it is called a&nbsp;sub-folder&nbsp;of it.<\/p>\n\n\n\n<p>The format of a path is&nbsp;one or more concatenated strings of the form:&nbsp;<code>\/<\/code>&nbsp;followed by one or more lowercase English letters. For example,&nbsp;<code>\/leetcode<\/code>&nbsp;and&nbsp;<code>\/leetcode\/problems<\/code>&nbsp;are valid paths while an empty string and&nbsp;<code>\/<\/code>&nbsp;are not.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> folder = [\"\/a\",\"\/a\/b\",\"\/c\/d\",\"\/c\/d\/e\",\"\/c\/f\"]\n<strong>Output:<\/strong> [\"\/a\",\"\/c\/d\",\"\/c\/f\"]\n<strong>Explanation:<\/strong> Folders \"\/a\/b\/\" is a subfolder of \"\/a\" and \"\/c\/d\/e\" is inside of folder \"\/c\/d\" in our filesystem.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> folder = [\"\/a\",\"\/a\/b\/c\",\"\/a\/b\/d\"]\n<strong>Output:<\/strong> [\"\/a\"]\n<strong>Explanation:<\/strong> Folders \"\/a\/b\/c\" and \"\/a\/b\/d\/\" will be removed because they are subfolders of \"\/a\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> folder = [\"\/a\/b\/c\",\"\/a\/b\/ca\",\"\/a\/b\/d\"]\n<strong>Output:<\/strong> [\"\/a\/b\/c\",\"\/a\/b\/ca\",\"\/a\/b\/d\"]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= folder.length&nbsp;&lt;= 4 * 10^4<\/code><\/li><li><code>2 &lt;= folder[i].length &lt;= 100<\/code><\/li><li><code>folder[i]<\/code>&nbsp;contains only&nbsp;lowercase letters and &#8216;\/&#8217;<\/li><li><code>folder[i]<\/code>&nbsp;always starts with character &#8216;\/&#8217;<\/li><li>Each folder name is unique.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*L)<br>Space complexity: O(n*L)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<string> removeSubfolders(vector<string>& folder) {\n    unordered_set<string> s(begin(folder), end(folder));\n    vector<string> ans;\n    for (const auto& cur : folder) {\n      string f = cur;\n      bool valid = true;\n      while (!f.empty() && valid) {\n        while (f.back() != '\/') f.pop_back();\n        f.pop_back();\n        if (s.count(f)) valid = false;\n      }\n      if (valid) ans.push_back(cur);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a list of folders, remove all sub-folders in those folders and return in&nbsp;any order&nbsp;the folders after removing. If a&nbsp;folder[i]&nbsp;is located within&nbsp;another&nbsp;folder[j], it is called&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[512,177,116,4],"class_list":["post-5776","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hsahtable","tag-medium","tag-path","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5776"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5776\/revisions"}],"predecessor-version":[{"id":5778,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5776\/revisions\/5778"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}