Imagine you're organizing a computer filesystem and need to clean up redundant folder paths. You have a list of folder paths, but some folders are actually sub-folders of others, creating unnecessary clutter.
Your task is to remove all sub-folders and keep only the root-level folders that aren't contained within any other folder in the list.
What makes a sub-folder? A folder is considered a sub-folder if:
- It starts with another folder's path
- Followed by a forward slash
/ - Then additional path components
Example: "/a/b" is a sub-folder of "/a", but "/b" is NOT a sub-folder of "/a/b/c"
Goal: Given a list of folder paths, return only the folders that aren't sub-folders of any other folder in the list. The order of the result doesn't matter.
Input & Output
Visualization
Time & Space Complexity
n² for nested loops comparing all pairs, m for average string length comparison
Only using a few variables, not counting the output array
Constraints
- 1 ≤ folder.length ≤ 4 × 104
- 2 ≤ folder[i].length ≤ 100
- folder[i] contains only lowercase letters and '/'
- folder[i] always starts with the character '/'
- Each folder name is unique