Remove Sub-Folders from the Filesystem
Remove Sub-Folders from the Filesystem
Given a list of file system folder paths, return a new list containing only the root folders, removing any paths that are sub-folders of other paths in the input list. A path is considered a sub-folder if it starts with another path in the list, followed immediately by a directory separator ('/').
For example, "/a/b" is a sub-folder of "/a", but "/b" is not a sub-folder of "/a/b/c".
Constraints:
- The input list contains between 1 and 40,000 folder paths.
- Each folder path has a length between 2 and 100 characters.
- Folder paths consist only of lowercase English letters and the directory separator '/'.
- All folder paths begin with '/'.
- Each folder path is unique.
Example 1:
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
Output: ["/a","/c/d","/c/f"]
Example 2:
Input: folder = ["/a","/a/b/c","/a/b/d"]
Output: ["/a"]
Example 3:
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
Output: ["/a/b/c","/a/b/ca","/a/b/d"]
Similar LeetCode problem: Remove Sub-Folders from the Filesystem
AI interview prep
Core dilemma
Efficiently identifying and filtering hierarchical relationships (parent-child) within a large, flat list of string paths, while managing potential performance bottlenecks related to string operations and data structure choices.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Remove Sub-Folders from the Filesystem
Remove Sub-Folders from the Filesystem
Given a list of file system folder paths, return a new list containing only the root folders, removing any paths that are sub-folders of other paths in the input list. A path is considered a sub-folder if it starts with another path in the list, followed immediately by a directory separator ('/').
For example, "/a/b" is a sub-folder of "/a", but "/b" is not a sub-folder of "/a/b/c".
Constraints:
- The input list contains between 1 and 40,000 folder paths.
- Each folder path has a length between 2 and 100 characters.
- Folder paths consist only of lowercase English letters and the directory separator '/'.
- All folder paths begin with '/'.
- Each folder path is unique.
Example 1:
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
Output: ["/a","/c/d","/c/f"]
Example 2:
Input: folder = ["/a","/a/b/c","/a/b/d"]
Output: ["/a"]
Example 3:
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
Output: ["/a/b/c","/a/b/ca","/a/b/d"]
Similar LeetCode problem: Remove Sub-Folders from the Filesystem