Skip to content
Problem 609 · LeetCode 609(opens in a new tab)

Find Duplicate File in System

MediumHash tables

A file system is described by paths, a list of strings. Each string names one directory and then lists the files inside it, separated by single spaces. A file is written as its name followed by its content in round brackets, like notes.txt(hello). A directory string looks like root/x/y a.txt(abc) b.txt(xyz).

Find every group of two or more files that have exactly the same content. Return each group as a list of full file paths, written as the directory, a /, and the file name. The groups, and the paths inside each group, may come in any order.

Example 1

Input:  paths = ["home a.txt(cat) b.txt(dog)", "home/pics c.txt(cat)",
                 "home/docs d.txt(dog) e.txt(owl)"]
Output: [["home/a.txt", "home/pics/c.txt"], ["home/b.txt", "home/docs/d.txt"]]
Why:    cat and dog appear twice each; owl only once
2 more examples and the constraints

Example 2

Input:  paths = ["r x.txt(1) y.txt(2)"]
Output: []
Why:    the two files have different content

Example 3

Input:  paths = ["box p.txt(zz) q.txt(zz)"]
Output: [["box/p.txt", "box/q.txt"]]
Why:    duplicates can sit in the same directory

Constraints

  • 1 ≤ length of paths ≤ 20,000
  • 1 ≤ length of each string ≤ 3,000, and all strings together are at most 500,000 characters
  • Strings use letters, digits, /, ., (, ) and spaces
  • No two files in one directory share a name, and no directory appears twice

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Compare every pair of filesO(F²·L)
  2. Group by content in a hash mapO(T)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100,000 filesVerdict
Compare every pair of filesO(F²·L)
Billions of string comparisons
Group by content in a hash mapO(T)
Best; one read of the input

F = number of files, L = longest content, T = total length of the input.

Edge cases to test

  • No duplicates. Every list has one path, so return an empty list.
  • Duplicates in one directory. Two files in the same folder can match; Example 3 shows it.
  • The root directory. A line may name just root with no subfolders; the path is still folder + "/" + name.
  • More than two copies. A group holds every matching path, not only pairs.

Common mistakes

  • Returning groups of size one, which are not duplicates.
  • Keying by file name instead of content; different folders can reuse a name.
  • Leaving the closing bracket in the content, or dropping the "/" between folder and name.

How to talk through it

  1. 1Clarify. Do equal names matter, or only equal content? Is the order of groups free?
  2. 2Parse first. Split on spaces; the first piece is the folder, each other piece is name(content).
  3. 3Give the pair check. Comparing every two files works but is quadratic in the number of files.
  4. 4Group by content. A map from content to paths does it in one pass; keep lists of size two or more.
  5. 5Test out loud. Run no duplicates, duplicates in one folder, and three copies of one file.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?