Ctrl K

Outermost Folders Only

Medium
A backup job is set up with a list of folders paths. If a folder sits inside another folder from the same list, there is no need to back it up separately: it gets copied anyway. Folder b is inside folder a if the path b starts with a immediately followed by /. For example, /x/y is inside /x, but /xy is not. Each path is one or more pieces of the form / + a non-empty name made of lowercase English letters, digits and _. All paths are distinct. Return the folders from the list that are not inside any other folder of the list, in any order.
Example 1
paths=['/home/anna', '/srv', '/home/anna/music', '/srv/www/static', '/home/bob'] → ['/home/anna', '/home/bob', '/srv']

/home/anna/music is inside /home/anna and /srv/www/static is inside /srv, so both go.

Example 2
paths=['/log', '/logs', '/log/old'] → ['/log', '/logs']

/logs starts with /log, but it is a different folder, not a nested one: a nested path continues with /.

Constraints
  • 1 ≤ len(paths) ≤ 4·10⁴
  • 2 ≤ len(paths[i]) ≤ 100
  • Paths consist of lowercase English letters, digits, _ and /, start with / and do not end with it
  • All paths are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWords Built From Shorter Words HardWord Traced on a Letter Grid MediumSplit Into Dictionary Words MediumRatio Chain Queries MediumHighlight Search Hits Medium
loading Python…
Input
paths=['/home/anna', '/srv', '/home/anna/music', '/srv/www/static', '/home/bob']
Expected
['/home/anna', '/home/bob', '/srv']

Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.