LeetCode 1233. Remove Sub-Folders from the Filesystem 解题报告(python)

1233. Remove Sub-Folders from the Filesystem

  1. Remove Sub-Folders from the Filesystem python solution

题目描述

Given a list of folders, remove all sub-folders in those folders and return in any order the folders after removing.
If a folder[i] is located within another folder[j], it is called a sub-folder of it.
The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, /leetcode and /leetcode/problems are valid paths while an empty string and / are not.

在这里插入图片描述

解析

首先将folder按长度排序,短的在前面,长的在后面。
然后检查每个每个路径是否存在父类,若存在就跳过。若不存在,就将其添加入answer数组内。

class Solution:
    def removeSubfolders(self, folder: List[str]) -> List[str]:
        folder.sort(key=len)
        seen = set()
        for f in folder:
            if not any(f[i] == '/' and f[: i] in seen for i in range(2, len(f))):
                seen.add(f)
        return list(seen)
        

Reference

https://leetcode.com/problems/remove-sub-folders-from-the-filesystem/discuss/409028/JavaPython-3-3-methods-from-O(NlogN)-to-O(N)-w-brief-explanation-and-analysis.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值