Problem statement

https://leetcode.com/problems/remove-sub-folders-from-the-filesystem/

Solution

One way to solve this problem is to sort our folders and then check that next folder do not start with previous name.

Complexity

It is O(n log n m) for time, where n = len(folder) and m is the average length of folder.

Code

class Solution:
    def removeSubfolders(self, folder):
        ans = ["?"]
        for f in sorted(folder):
            if not f.startswith(ans[-1] + '/'):
                ans += [f]
        return ans[1:]

Remark

There is also Tries solution with O(nm) space.