#1948

Delete Duplicate Folders in System

specialist · 920 · lc hard +32 · premium · verified · 77.7% accepted · 619 likes · top 89%

Description

You are given a 2D array paths where each paths[i] is a list of folder names forming an absolute path. Two folders are identical if they contain the exact same set of subfolders (compared recursively).

When two or more identical folders (and all their subfolders) are found, mark them for deletion. After the deletion, do not revisit any newly identical folders.

Return all remaining folder paths.

Example 1:

Input: paths = [["a"],["c"],["d"],["a","b"],["c","b"],["d","a"]]
Output: [["d"],["d","a"]]
Explanation: The file structure is as shown.
Folders "/a" and "/c" (and their subfolders) are marked for deletion because they both contain an empty
folder named "b".

Example 2:

Input: paths = [["a"],["c"],["a","b"],["c","b"],["a","b","x"],["a","b","x","y"],["w"],["w","y"]]
Output: [["c"],["c","b"],["a"],["a","b"]]
Explanation: The file structure is as shown.
Folders "/a/b/x" and "/w" (and their subfolders) are marked for deletion because they both contain an empty folder named "y".
Note that folders "/a" and "/c" are identical after the deletion, but they are not deleted because they were not marked beforehand.

Example 3:

Input: paths = [["a","b"],["c","d"],["c"],["a"]]
Output: [["c"],["c","d"],["a"],["a","b"]]
Explanation: All folders are unique in the file system.
Note that the returned array can be in a different order as the order does not matter.

Code

1
2
3