Problem
You are given a list of directory descriptions. Each description contains a directory path followed by one or more file records of the form filename(content). Return every group of files with exactly equal contents. Include all full paths belonging to each group and omit groups containing only one file. A full path is directory/filename.
Both the order of the groups and the order of paths within each group may be arbitrary. The input already contains file contents; this task does not read an actual filesystem.
Input Grammar
Use this explicit interpretation of the source's directory/file format:
description = directory SPACE file_record (SPACE file_record)*
file_record = filename "(" content ")"
The directory may be just root or a nested path such as root/a/b. A directory path contains no spaces; parentheses in the directory path are ordinary path characters. A filename contains no spaces or parentheses. A single space outside parentheses separates the directory from its first file record and separates successive file records.
Content may contain spaces, including consecutive, leading or trailing spaces. Preserve every content character exactly: a b, ab and a b are different contents. Parentheses are structural delimiters in this grammar, so content contains no literal parentheses. The grammar also parses file.txt() as empty content.
The source does not define an escaping or nesting convention for literal parentheses inside filenames or contents. Such a convention would need to be specified before supporting that additional variant. Do not assume that a space inside parentheses ends a file record. Inputs are well formed under the grammar above; malformed-input handling is not required.
Examples
Example 1
Input: ["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"]]
The content efgh occurs in three files, and abcd occurs in two. Both complete groups must appear.
Example 2
Input: ["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"]]
There are two groups of two files. If no content appears more than once, return an empty list.
Example 3: spaces inside content
Input: ["root a.txt(a b) b.txt(a b) c.txt(ab) d.txt(a b)"]
Output: [["root/a.txt", "root/b.txt"]]
The first two files contain the same three-character string a b. Neither ab nor a b is equal to that content.
Constraints
1 <= len(paths) <= 20,000.
- Each description has
1..3000 characters, and the combined length is 1..500,000 characters.
- Input characters are English letters, digits,
/, ., (, ) and spaces, subject to the grammar above.
- Each description represents a different directory and contains at least one file record.
- Within a directory, no two files or directories share the same name.
Interface and Existing Console
The original source uses Solution.findDuplicate(paths). The written Python reference below provides a standalone solution(descriptions) function so that its complete implementation can be tested in isolation; its argument is the same list of descriptions.
The existing console on this page is a legacy version. It uses solution(descriptions), limits input to 10,000 descriptions and 100,000 files, and requires sorted paths within groups and groups sorted by their first path. Its saved Python reference splits descriptions on whitespace and does not correctly handle the space-containing content above.
This written exercise preserves the broader source bounds and arbitrary output order. It does not change the legacy console or certify its grader, tests or references for this grammar. Use the written exercise independently; a successful legacy-console submission does not establish coverage of these requirements.
What a Strong Answer Covers
- Return every member of every duplicate-content group, including different filenames with equal contents.
- Construct full paths correctly for root and nested directories.
- Preserve spaces inside content and distinguish content equality from equal names or equal sizes.
- Omit singleton groups and accept arbitrary ordering at both levels.
- Account for parsing and the storage required by all constructed full paths.
Follow-up Questions
- For a real filesystem, would you discover files with DFS or BFS, and why?
- How would your approach change for gigabyte-sized files?
- What would you change if each read could return at most 1 KB?
- What are the time and space costs of that filesystem approach? Which operations consume the most time and memory, and how would you reduce those costs?
- How would you ensure that the reported duplicates are not false positives?