Given a list of folders folder
, return the folders after removing all sub-folders in those folders. You may return the answer in any order.
If a folder[i]
is located within another folder[j]
, it is called a sub-folder of it. A sub-folder of folder[j]
must start with folder[j]
, followed by a "/"
. For example, "/a/b"
is a sub-folder of "/a"
, but "/b"
is not a sub-folder of "/a/b/c"
.
The format of a path is one or more concatenated strings of the form: '/'
followed by one or more lowercase English letters.
"/leetcode"
and "/leetcode/problems"
are valid paths while an empty string and "/"
are not.Example 1:
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"] Output: ["/a","/c/d","/c/f"] Explanation: Folders "/a/b" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.
Example 2:
Input: folder = ["/a","/a/b/c","/a/b/d"] Output: ["/a"] Explanation: Folders "/a/b/c" and "/a/b/d" will be removed because they are subfolders of "/a".
Example 3:
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"] Output: ["/a/b/c","/a/b/ca","/a/b/d"]
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 '/'
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method is like checking every possible combination to find the answer. For this problem, it involves comparing each folder to every other folder to see if one is contained inside the other.
Here's how the algorithm would work step-by-step:
def remove_subfolders_brute_force(folder_list):
result = []
for current_folder in folder_list:
is_subfolder = False
for other_folder in folder_list:
# Avoid comparing folder to itself
if current_folder == other_folder:
continue
# Check if current folder is a subfolder
if other_folder.startswith(current_folder + "/"):
is_subfolder = True
# If current folder is subfolder, break early
break
# Add the current folder to result list
if not is_subfolder:
result.append(current_folder)
return result
The key idea is to treat the folder paths like words in a dictionary. We want to quickly identify and remove the 'child' folders, which are simply longer paths that start with a shorter 'parent' path. Sorting the paths makes this process very efficient.
Here's how the algorithm would work step-by-step:
def remove_subfolders(folder):
folder.sort()
result = []
previous_parent = ""
for current_folder in folder:
# Check if the current folder is a subfolder of the previous parent.
if not current_folder.startswith(previous_parent):
result.append(current_folder)
# Update the previous parent to the current folder.
previous_parent = current_folder + "/"
return result
Case | How to Handle |
---|---|
Empty input list | Return an empty list as there are no folders to process. |
List with a single folder | Return the list as is, since a single folder cannot be a sub-folder of another. |
List with identical folder paths | The solution should return the duplicate paths only once. |
Folder paths with trailing slashes | The solution should normalize the paths by removing trailing slashes before comparison. |
Folder paths that are prefixes of each other but not direct subfolders (e.g., 'a/b' and 'a/bc') | The algorithm must only identify as sub-folders those paths that begin with another folder's path + '/'. |
Very long folder path strings | The solution should handle potentially long strings without causing performance issues or memory overflow, using efficient string comparison methods. |
Folder paths containing special characters or unicode characters | The algorithm should correctly handle special characters and Unicode characters within the folder paths by using appropriate string comparison functions. |
A folder being a subfolder of multiple other folders. | The subfolder should be removed regardless of how many root folders include it. |