← 返回 anthropic 的题目列表Find Duplicate Files by Content
类型:online_judge
Problem: Find Duplicate Files by Content
Given a snapshot of a file system containing n files. Each file has a unique absolute path and its content. Find all groups of files whose contents are exactly the same, and output their paths by group.
In the actual interview, this may be asked as traversing a real file system directory, e.g. implementing find_duplicates(root_dir). For testability, this version represents the file system snapshot through standard input.
Input Format
The first line contains an integer n, the number of files.
The next n lines each represent one file in the following format:
<path>\t<content>
<path> is an absolute path without tab characters. <content> is the file content string and may be empty.
Output Format
Output all duplicate file groups.
Only groups with at least 2 files should be output.
Paths inside each group should be sorted lexicographically and separated by a single space.
Groups should be sorted by the first path in each group.
If there are no duplicate files, output nothing.
Constraints
0 <= n <= 10^5
Total path length is at most 10^7.
Total content length is at most 10^8.
In a real file system, files can be very large, so you should not assume all file contents can be loaded into memory at once.
Follow-ups may include hash collisions, permission errors, files changing during reads, large-file efficiency, and distributed processing.
Example
Input:
5
/a/1.txt\thello
/a/2.txt\tworld
/b/3.txt\thello
/c/4.txt\tfoo
/d/5.txt\tworld
Output:
/a/1.txt /b/3.txt
/a/2.txt /d/5.txt
Example
Input
5
/a/1.txt hello
/a/2.txt world
/b/3.txt hello
/c/4.txt foo
/d/5.txt world
Output
/a/1.txt /b/3.txt
/a/2.txt /d/5.txt