609. Find Duplicate File in System - cocoder39/coco39_LC GitHub Wiki

609. Find Duplicate File in System

class Solution:
    def findDuplicate(self, paths: List[str]) -> List[List[str]]:
        content_to_path = collections.defaultdict(list)
        for path in paths:
            tokens = path.split(' ')
            directory = tokens[0]
            for i in range(1, len(tokens)):
                file = tokens[i]
                segments = file.split('(')
                content = segments[1]
                file_path = directory + '/' + segments[0]
                content_to_path[content].append(file_path)
        res = []
        for _, paths in content_to_path.items():
            if len(paths) > 1:
                res.append(paths)
        return res