<span>leetcode-609 Find Duplicate File in System</span>

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths.

A group of duplicate files consists of at least two files that have exactly the same content.

A single directory info string in the input list has the following format:

"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"

It means there are n files (f1.txtf2.txt ... fn.txt with content f1_contentf2_content ... fn_content, respectively) in directory root/d1/d2/.../dm. Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.

The output is a list of group of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:

"directory_path/file_name.txt"

输入输出实例:

Input:
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
Output:  
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]



本题题意比较简单,就是找出含有重复内容的文件归类输出就行了。
class Solution:
    def findDuplicate(self, paths: List[str]) -> List[List[str]]:
        def seperateStr(path):
            return path.split()
        dic = {}
        for path in paths:
            pa = seperateStr(path)
            pa[0] = pa[0] + "/"
            prefix = pa[0]
            for i in range(1,len(pa)):
                item = pa[i].find("(")
                suffix = pa[i][:item]
                content = pa[i][item+1:len(pa[i])-1]
                if content in dic:
                    dic[content].append(prefix + suffix)
                else:
                    dic[content] = [prefix + suffix]
        result = []
        for i in dic:
            if len(dic[i]) > 1:
                result.append(dic[i])
        return result
                

 

 
全部评论

相关推荐

德科信息 华为OD岗位 20K+ 统招本科
点赞 评论 收藏
分享
牛客389580366号:读书的意义在于提升自己和他人吧,“阶级意识”是读书过程中的产出,“跨越阶级”是通过读书获得的能力
点赞 评论 收藏
分享
冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务