609. Find Duplicate File in System

Information

  • Diffculty: Medium

  • Created: 2019-08-29 21:49:57

  • Last Motified: 2019-08-29 21:49:57

Solution

class Solution:
    def findDuplicate(self, paths: List[str]) -> List[List[str]]:
        filepaths = {}
        for p in paths:
            spl = p.split(' ')
            directory = spl[0]
            for f in spl[1:]:
                name = f[:f.index('(')]
                content = f[f.index('(')+1:f.index(')')]
                if content not in filepaths:
                    filepaths[content] = []
                filepaths[content].append("%s/%s" % (directory, name))

        ans = []
        for group in filepaths.values():
            if len(group) > 1:
                ans.append(group)
        return ans