Remove Sub-Folders from the Filesystem

Given a list of folder paths `folder`, remove every folder that lies inside another folder of the list and return the remaining folders in any order. A folder `folder[i]` is a sub-folder of `folder[j]` when it starts 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"`. Each path is one or more `"/"` followed by a name of lowercase letters, such as `"/leetcode"` or `"/leetcode/problems"`.

30:00

Remove Sub-Folders from the Filesystem
medium
Free written solution
Topics
Companies

Given a list of folder paths folder, remove every folder that lies inside another folder of the list and return the remaining folders in any order. A folder folder[i] is a sub-folder of folder[j] when it starts 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". Each path is one or more "/" followed by a name of lowercase letters, such as "/leetcode" or "/leetcode/problems".

Example 1:
Input: {"folder":["/a","/a/b","/c/d","/c/d/e","/c/f"]}
Output: ["/a","/c/d","/c/f"]
Constraints:
  • 1≤folder.length≤4×1041 \leq \text{folder.length} \leq 4 \times 10^4

  • 2≤folder[i].length≤1002 \leq \text{folder}[i].\text{length} \leq 100

  • folder[i] contains only lowercase letters and /.

  • folder[i] always starts with /.

  • Each folder name is unique.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"folder":["/a","/a/b","/c/d","/c/d/e","/c/f"]}

5 folders arrive in no particular order, so a sub-folder can appear before or after the folder that contains it.

input folders (unsorted)

/a
/a/b
/c/d
/c/d/e
/c/f

result (0)

[ ]

kept 0
dropped 0
pending 5
Variables
VariableValue
len(folder)5
DepthFunction Call
Stack empty
0/8