<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
                

 

 
全部评论

相关推荐

2024-12-02 22:30
西安理工大学 Java
M_bao:简历排版换一下吧,第二个项目换了吧,咱门双非学历本来就不行还用这种项目太掉分了,300沟通一个要简历你打招呼也有问题。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务