|  | 
|  | 1 | +<p>Due to a bug, there are many duplicate folders in a file system. You are given a 2D array <code>paths</code>, where <code>paths[i]</code> is an array representing an absolute path to the <code>i<sup>th</sup></code> folder in the file system.</p> | 
|  | 2 | + | 
|  | 3 | +<ul> | 
|  | 4 | +	<li>For example, <code>["one", "two", "three"]</code> represents the path <code>"/one/two/three"</code>.</li> | 
|  | 5 | +</ul> | 
|  | 6 | + | 
|  | 7 | +<p>Two folders (not necessarily on the same level) are <strong>identical</strong> if they contain the <strong>same non-empty</strong> set of identical subfolders and underlying subfolder structure. The folders <strong>do not</strong> need to be at the root level to be identical. If two or more folders are <strong>identical</strong>, then <strong>mark</strong> the folders as well as all their subfolders.</p> | 
|  | 8 | + | 
|  | 9 | +<ul> | 
|  | 10 | +	<li>For example, folders <code>"/a"</code> and <code>"/b"</code> in the file structure below are identical. They (as well as their subfolders) should <strong>all</strong> be marked: | 
|  | 11 | + | 
|  | 12 | +	<ul> | 
|  | 13 | +		<li><code>/a</code></li> | 
|  | 14 | +		<li><code>/a/x</code></li> | 
|  | 15 | +		<li><code>/a/x/y</code></li> | 
|  | 16 | +		<li><code>/a/z</code></li> | 
|  | 17 | +		<li><code>/b</code></li> | 
|  | 18 | +		<li><code>/b/x</code></li> | 
|  | 19 | +		<li><code>/b/x/y</code></li> | 
|  | 20 | +		<li><code>/b/z</code></li> | 
|  | 21 | +	</ul> | 
|  | 22 | +	</li> | 
|  | 23 | +	<li>However, if the file structure also included the path <code>"/b/w"</code>, then the folders <code>"/a"</code> and <code>"/b"</code> would not be identical. Note that <code>"/a/x"</code> and <code>"/b/x"</code> would still be considered identical even with the added folder.</li> | 
|  | 24 | +</ul> | 
|  | 25 | + | 
|  | 26 | +<p>Once all the identical folders and their subfolders have been marked, the file system will <strong>delete</strong> all of them. The file system only runs the deletion once, so any folders that become identical after the initial deletion are not deleted.</p> | 
|  | 27 | + | 
|  | 28 | +<p>Return <em>the 2D array </em><code>ans</code> <em>containing the paths of the <strong>remaining</strong> folders after deleting all the marked folders. The paths may be returned in <strong>any</strong> order</em>.</p> | 
|  | 29 | + | 
|  | 30 | +<p> </p> | 
|  | 31 | +<p><strong class="example">Example 1:</strong></p> | 
|  | 32 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder1.jpg" style="width: 200px; height: 218px;" /> | 
|  | 33 | +<pre> | 
|  | 34 | +<strong>Input:</strong> paths = [["a"],["c"],["d"],["a","b"],["c","b"],["d","a"]] | 
|  | 35 | +<strong>Output:</strong> [["d"],["d","a"]] | 
|  | 36 | +<strong>Explanation:</strong> The file structure is as shown. | 
|  | 37 | +Folders "/a" and "/c" (and their subfolders) are marked for deletion because they both contain an empty | 
|  | 38 | +folder named "b". | 
|  | 39 | +</pre> | 
|  | 40 | + | 
|  | 41 | +<p><strong class="example">Example 2:</strong></p> | 
|  | 42 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder2.jpg" style="width: 200px; height: 355px;" /> | 
|  | 43 | +<pre> | 
|  | 44 | +<strong>Input:</strong> paths = [["a"],["c"],["a","b"],["c","b"],["a","b","x"],["a","b","x","y"],["w"],["w","y"]] | 
|  | 45 | +<strong>Output:</strong> [["c"],["c","b"],["a"],["a","b"]] | 
|  | 46 | +<strong>Explanation: </strong>The file structure is as shown.  | 
|  | 47 | +Folders "/a/b/x" and "/w" (and their subfolders) are marked for deletion because they both contain an empty folder named "y". | 
|  | 48 | +Note that folders "/a" and "/c" are identical after the deletion, but they are not deleted because they were not marked beforehand. | 
|  | 49 | +</pre> | 
|  | 50 | + | 
|  | 51 | +<p><strong class="example">Example 3:</strong></p> | 
|  | 52 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/07/19/lc-dupfolder3.jpg" style="width: 200px; height: 201px;" /> | 
|  | 53 | +<pre> | 
|  | 54 | +<strong>Input:</strong> paths = [["a","b"],["c","d"],["c"],["a"]] | 
|  | 55 | +<strong>Output:</strong> [["c"],["c","d"],["a"],["a","b"]] | 
|  | 56 | +<strong>Explanation:</strong> All folders are unique in the file system. | 
|  | 57 | +Note that the returned array can be in a different order as the order does not matter. | 
|  | 58 | +</pre> | 
|  | 59 | + | 
|  | 60 | +<p> </p> | 
|  | 61 | +<p><strong>Constraints:</strong></p> | 
|  | 62 | + | 
|  | 63 | +<ul> | 
|  | 64 | +	<li><code>1 <= paths.length <= 2 * 10<sup>4</sup></code></li> | 
|  | 65 | +	<li><code>1 <= paths[i].length <= 500</code></li> | 
|  | 66 | +	<li><code>1 <= paths[i][j].length <= 10</code></li> | 
|  | 67 | +	<li><code>1 <= sum(paths[i][j].length) <= 2 * 10<sup>5</sup></code></li> | 
|  | 68 | +	<li><code>path[i][j]</code> consists of lowercase English letters.</li> | 
|  | 69 | +	<li>No two paths lead to the same folder.</li> | 
|  | 70 | +	<li>For any folder not at the root level, its parent folder will also be in the input.</li> | 
|  | 71 | +</ul> | 
0 commit comments