Delete Duplicate Folders in System - Complete Solution Guide
Delete Duplicate Folders in System is LeetCode problem 1948, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 i th folder in the file system. For example, ["one", "two", "three"] represents the path "/one/two/three" . 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 t
Detailed Explanation
The problem asks us to identify and delete duplicate folders in a file system represented by a 2D array of paths. Two folders are considered identical if they contain the same set of identical subfolders with the same structure. The deletion process involves marking identical folders and all their subfolders, and then removing them from the file system. The goal is to return a list of the remaining folder paths after the deletion, in any order.
Solution Approach
The provided solution uses a Trie data structure to represent the file system. The algorithm first builds the Trie from the input paths. Then, it recursively calculates a unique signature for each folder based on its subfolders. Identical folders will have the same signature. The solution then identifies folders with duplicate signatures and marks them and their subfolders for deletion. Finally, it traverses the Trie again to collect the paths of the remaining (non-deleted) folders.
Step-by-Step Algorithm
- Step 1: **Build the Trie:** Iterate through the input `paths` and construct a Trie data structure. Each node in the Trie represents a folder, and the edges represent the folder hierarchy.
- Step 2: **Calculate Signatures:** Recursively traverse the Trie, starting from the root. For each node (folder), calculate a signature based on its children. The signature includes the sorted names of the children folder and their respective signature ids. The sorting ensures the signature is order-independent.
- Step 3: **Identify Duplicates:** Store the calculated signatures and their corresponding Trie nodes. Detect folders having the same signature ID. These indicate identical subfolder structure.
- Step 4: **Mark for Deletion:** If multiple folders have the same signature (meaning they are duplicates), mark these folders and all their descendant folders in the Trie as `deleted`.
- Step 5: **Collect Remaining Paths:** Traverse the Trie again. This time, collect the paths of the folders that are *not* marked as `deleted`. These are the folders that remain after removing the duplicates.
- Step 6: **Return Result:** Return the collected paths as a 2D array.
Key Insights
- Insight 1: Representing the file system as a Trie data structure allows efficient traversal and identification of folders based on their paths.
- Insight 2: The core challenge is defining a robust and efficient way to determine if two folders are identical, which depends on the structure and contents of their subfolders. We can use a unique 'signature' based on children folder name and child signature id to identify identical folder structure
- Insight 3: Sorting the subfolders' names before calculating the signature ensures that the order of subfolders doesn't affect the signature, which is crucial for determining identical folders.
Complexity Analysis
Time Complexity: O(N*L)
Space Complexity: O(N*L)
Topics
This problem involves: Array, Hash Table, String, Trie, Hash Function.
Companies
Asked at: Booking.com, Nvidia.