<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
                

 

 
全部评论

相关推荐

秋招投简历提醒助手:个人经验是,一般面二十场左右就会进入侃侃而谈阶段。我今年七月末的时候开始的第一次面试,都是很多不会,回复很慢。后面慢慢迭代,到九月中的时候基本上面啥说啥,很放松的状态
远程面试的尴尬瞬间
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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