Loading...
Loading...
Your laptop is full before the placement season, and you suspect many notes files are copies. You list n files, each with its name and its whole contents.
Find every group of two or more files whose contents are exactly the same. Inside a group, sort the file names in dictionary order. Then sort the groups by their first file name.
Print the number of groups, then each group on its own line.
0.Example 1
Input
5 notes.txt arrays123 copy.txt arrays123 oops.txt classObj old.txt arrays123 hw.txt classObj
Output
2 copy.txt notes.txt old.txt hw.txt oops.txt
Three files hold arrays123 and two hold classObj. Group copy.txt comes first because copy.txt is the smallest first name.
Example 2
Input
3 a.txt java b.txt Java c.txt jav
Output
0
Contents must match exactly, and capital letters count, so there are no groups.
Try the problem first. Open one hint at a time.
Comparing every pair of files is about n²/2 comparisons, which is 5 × 10^9 for n = 10^5.
Use the contents as the key of a HashMap, and keep the list of file names for each key.
Keep only lists with two or more names, sort each list, then sort the lists by their first name.
Group by the contents string itself, not by its hashCode(). Two different strings can have the same hash code.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor