<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
                

 

 
全部评论

相关推荐

09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务