Skip to content

Practice Delete Duplicate Folders in System

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1948
LeetCode Problem

Delete Duplicate Folders in System

Problem Statement

Due to a bug, there are many duplicate folders in a file system. You are given a 2D array paths, where paths[i] is an array representing an absolute path to the ith folder in the file system. Two folders (not necessarily on the same level) are identical if they contain the same non-empty set of identical subfolders and underlying subfolder structure. The folders do not need to be at the root level to be identical. If two or more folders are identical, then mark the folders as well as all their subfolders. Once all the identical folders and their subfolders have been marked, the file system will delete all of them. The file system only runs the deletion once, so any folders that become identical after the initial deletion are not deleted. Return the 2D array ans containing the paths of the remaining folders after deleting all the marked folders. The paths may be returned in any order.

Source: Delete Duplicate Folders in System on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
paths = [["a"],["c"],["d"],["a","b"],["c","b"],["d","a"]]
Output
[["d"],["d","a"]]

# 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"]]

# Example 3

Input
paths = [["a","b"],["c","d"],["c"],["a"]]
Output
[["c"],["c","d"],["a"],["a","b"]]

Constraints

1 <= paths.length <= 2 * 10^4
1 <= paths[i].length <= 500
1 <= paths[i][j].length <= 10
1 <= sum(paths[i][j].length) <= 2 * 10^5
path[i][j] consists of lowercase English letters.
No two paths lead to the same folder.
For any folder not at the root level, its parent folder will also be in the input.

Related LeetCode Problems

Practice more problems similar to Delete Duplicate Folders in System.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.