-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_609_find_duplicate_in_file_system.py
More file actions
30 lines (26 loc) · 1.33 KB
/
Copy pathleetcode_609_find_duplicate_in_file_system.py
File metadata and controls
30 lines (26 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#https://leetcode.com/problems/find-duplicate-file-in-system/description/
class Solution:
def findDuplicate(self, paths: list[str]) -> list[list[str]]:
contents = {}
def getFileContents(path: str) -> (str, str):
index = path.find('(')
return (path[0:index], path[index + 1: len(path) - 1])
for p in paths:
space_separated = p.split(' ')
# TODO: if file contains spaces this will not work
directory, files = space_separated[0], space_separated[1:]
for f in files:
(path, content) = getFileContents(f)
if content not in contents:
contents[content] = [f'{directory}/{path}']
else:
contents[content].append(f'{directory}/{path}')
result = []
for k, v in contents.items():
if len(v) >= 2:
result.append(v)
return result
#test cases
s = Solution()
assert s.findDuplicate(["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"]) == [["root/a/1.txt","root/c/3.txt"],["root/a/2.txt","root/c/d/4.txt","root/4.txt"]]
assert s.findDuplicate(["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"]) == [["root/a/1.txt","root/c/3.txt"],["root/a/2.txt","root/c/d/4.txt"]]