Given a list paths of directory info, including the directory path, and all the files with contents in this directory, return all the duplicate files in the file system in terms of their paths. You may return the answer in any order.
A group of duplicate files consists of at least two files that have the same content.
A single directory info string in the input list has the following format:
"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"It means there are n files (f1.txt, f2.txt ... fn.txt) with content (f1_content, f2_content ... fn_content) respectively in the directory "root/d1/d2/.../dm". Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.
The output is a list of groups of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:
"directory_path/file_name.txt"Example 1:
Input: paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"] Output: [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]
Example 2:
Input: paths = ["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)"] Output: [["root/a/2.txt","root/c/d/4.txt"],["root/a/1.txt","root/c/3.txt"]]
Constraints:
1 <= paths.length <= 2 * 1041 <= paths[i].length <= 30001 <= sum(paths[i].length) <= 5 * 105paths[i] consist of English letters, digits, '/', '.', '(', ')', and ' '.Follow up:
In #609 Find Duplicate File in System, each directory string contains file names and their contents in the format filename(content). The key idea is to identify files that share the same content. Instead of comparing every file with every other file (which would be inefficient), we can group files by their content.
A practical approach is to use a hash table where the key is the file content and the value is a list of full file paths containing that content. Parse each directory string, extract the directory path, file names, and their contents, and build the full file path for each file. Insert the path into the list mapped to its content.
After processing all files, iterate through the hash table and collect groups whose list size is greater than one. These groups represent duplicate files. The approach runs in roughly O(N × L) time where N is the number of files and L is the average string length, with additional space used for the hash map.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Hash Map Grouping by File Content | O(N × L) | O(N × L) |
Algorithms Made Easy
To find duplicate files by their content, we can use a HashMap (or Dictionary). For each directory info string, parse the directory path and the files with their contents. Use the content as the key in the map and store full path of the file as its value. After parsing all inputs, the map keys with more than one value represent duplicate files.
Time Complexity: O(n), where n is the total number of characters in all file paths. We iterate over each character once.
Space Complexity: O(n) to store the lists of file paths in the dictionary.
1import java.util.*;
2
3public class DuplicateFiles {
4 public List<List<String>> findDuplicate(String[]In Java, we use a HashMap to collect file paths by their content. Using the split method, we separate the root directory from the files. In a loop, the file name and contents are isolated, enabling us to map the full path of each file under its content as a key. Finally, groups with more than one entry are added to the result list and returned.
This approach is based on directly processing string data and arranging results using a 2D array. Strings are manipulated to extract directory data, file names, and contents into standalone variables, then append paths to a growing structure. Compared to hash maps, this method uses arrays to aggregate identical files.
Time Complexity: O(n), where n is the total input character count due to one-pass evaluation.
Space Complexity: O(n), maintaining paths and intermediate arrays.
1def find_duplicate_with_arrays(
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of this problem can appear in technical interviews at large tech companies. It tests string parsing, hash map usage, and the ability to design efficient grouping strategies for large datasets.
A hash table (or dictionary) is the best data structure for this problem. It allows efficient grouping of file paths based on identical file content and provides constant-time average insertion and lookup.
The optimal approach uses a hash map to group file paths by their content. As you parse each directory string, store the file content as the key and append the full file path to the corresponding list. Any group with more than one path represents duplicate files.
Duplicate files are defined by having the same content, not necessarily the same name. Two files may have different names but identical content, so grouping by content ensures all duplicates are correctly detected.
By substituting hash maps with straightforward string operations and arrays in Python, this solution mimics hash map behavior to achieve equivalent outcomes. It uses slicing to extract filenames and contents and builds a result by tracking paths directly through array indexing.