Leetcode 1233: Remove Sub-Folders from the Filesystem

grid47
grid47
Exploring patterns and algorithms
Jul 6, 2024 4 min read

You are given a list of folder paths, where each path represents a folder in the file system. The task is to remove all sub-folders from the list, keeping only the main folders.
Problem
Approach
Steps
Complexity
Input: The input is a list of folder paths. Each path starts with a '/' and may contain one or more sub-folder paths.
Example: folders = ['/home/user/docs', '/home/user', '/home/user/docs/photos', '/home/user/music']
Constraints:
• 1 <= folder.length <= 4 * 10^4
• 2 <= folder[i].length <= 100
• folder[i] contains only lowercase letters and '/'
• Each folder path is unique
Output: The output should be a list of folder paths with all sub-folders removed.
Example: ["/home/user", "/home/user/music"]
Constraints:
• The output should not contain any sub-folder paths.
Goal: The goal is to remove any folder that is a sub-folder of another folder in the list.
Steps:
• Sort the folder paths lexicographically.
• Use a trie structure to store and check for sub-folders.
• Add a folder to the result list only if it is not a sub-folder of any other folder.
Goal: The input contains unique folder paths, and the number of folder paths is between 1 and 40,000.
Steps:
• The input will not contain any duplicate folder paths.
Assumptions:
• Each folder path starts with a '/' and contains valid characters.
Input: folders = ['/home/user/docs', '/home/user', '/home/user/docs/photos', '/home/user/music']
Explanation: The folders '/home/user/docs' and '/home/user/docs/photos' are sub-folders of '/home/user', so they are removed. The remaining folders are '/home/user' and '/home/user/music'.

Input: folders = ['/a/b/c', '/a/b', '/a/c', '/a']
Explanation: The folders '/a/b/c', '/a/b', and '/a/c' are sub-folders of '/a', so only '/a' remains.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus