Skip to the code
cf.
+50 XP

#36 Duplicate Asset Contents

hard

A bundler wants to find assets with identical contents so it can ship them once. Each entry of listing describes one folder: the folder path, then one or more files written as name(content), all separated by single spaces. For example, "public logo.svg(abc) icon.svg(xyz)" means public/logo.svg holds abc and public/icon.svg holds xyz.

Write duplicateFiles(listing) that returns groups of full paths (folder + "/" + name) whose contents are identical, keeping only groups with at least two files.

Order the groups by where their content first appears in the listing (reading entries in order, files left to right), and list the paths inside each group in that same reading order.

Example 1

Input: listing = ["assets/img logo.svg(abc) icon.svg(xyz)","public logo.svg(abc)","src app.js(xyz) util.js(qq)"]
Output: [["assets/img/logo.svg","public/logo.svg"],["assets/img/icon.svg","src/app.js"]]

abc appears in two files, then xyz in two. qq appears once, so it's left out.

Example 2

Input: listing = ["root a.txt(1) b.txt(2)"]
Output: []

No two files share contents.

Constraints

  • 0 <= listing.length <= 1000
  • Folder paths, file names, and contents contain no spaces or parentheses.
  • Every entry has a folder and at least one file.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).