321. Find Duplicate File in System

Medium
Content Grouping
Hashing

Problem

You are given directory descriptions. Each description contains a directory path followed by one or more files represented as fileName(content). Return groups of complete file paths whose files have identical content. Only groups containing at least two files should be returned. Groups and paths within groups may be returned in any order.

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/1.txt","root/c/3.txt"],["root/a/2.txt","root/c/d/4.txt","root/4.txt"]]

Example 2

Input: ["root/a 1.txt(x)","root/b 2.txt(y)"]
Output: []

Example 3

Input: ["a x.txt(z)","b y.txt(z)"]
Output: [["a/x.txt","b/y.txt"]]
Constraints

1 <= paths.length <= 20000

Every file description contains a file name followed by its content in parentheses.

Hints

?? Parse each directory description into a directory and file tokens.

?? Use file content as the hash-map key.

?? Store complete paths for every content value and keep only groups of size at least two.

Expected Complexity
Time: O(total input characters)
Space: O(total input characters)
Follow-up

For a real file system containing huge files, why might hashing the entire content immediately be inefficient?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
["root/a 1.txt(abcd) 2.txt(efgh)","root/c 3.txt(abcd)","root/c/d 4.txt(efgh)","root 4.txt(efgh)"]
Expected
[["root/a/1.txt","root/c/3.txt"],["root/a/2.txt","root/c/d/4.txt","root/4.txt"]]