Skip to main content

609. Find Duplicate File in System

https://leetcode.com/problems/find-duplicate-file-in-system/

Python

  • Time: O(N) # N is amount of files
  • Space: O(N)
from os.path import join as pathjoin
from collections import defaultdict


class Solution:
def findDuplicate(self, paths: List[str]) -> List[List[str]]:
mapper = defaultdict(list)

for path in paths:
records = path.split(' ')

folder = records[0]
files = records[1:]

for file in files:
filename, content = file.split('(')
content = content[:-1]

mapper[content].append(pathjoin(folder, filename))

return [items for items in mapper.values() if len(items) > 1]