#1233

Remove Sub-Folders from the Filesystem

pupil · 465 · lc medium +26 · verified · 78.6% accepted · 1,655 likes · top 90%

Description

You are given a list of folder paths folder. Remove all entries that are sub-folders of another folder in the list. Return the remaining top-level folders in any order.

A path folder[i] is a sub-folder of folder[j] when folder[i] starts with folder[j] followed by "/". For example, "/a/b" is a sub-folder of "/a", but "/b" is not a sub-folder of "/a/b/c".

Each path consists of one or more segments of the form '/' followed by lowercase letters, e.g. "/leetcode" and "/leetcode/problems" are valid.

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"]

Code

1
2
3