This problem is based on LeetCode 609 (Find Duplicate File in System). If you haven’t solved that one yet, it’s recommended to do so first.
In a simulated file system rooted at "/", your task is to identify all groups of files that share exactly the same binary content.
Two files are considered duplicates only when their complete binary contents are byte-for-byte identical; file names and directory paths are irrelevant. The content may be large, and you can access it only as a binary stream—direct string access is not available. You need to design an efficient approach that can handle a large number of files and directories.
Implement the DuplicateFileFinder class:
DuplicateFileFinder(FileSystem fs)
Constructs an instance that will operate on the given file system fs.
List<List<String>> findDuplicateFiles()
Explores the file system starting from the root ("/") and returns a list of duplicate groups:
The following FileSystem interface is provided; do not modify it. You can use its methods to list directory contents, determine whether a path is a directory, obtain file sizes, and open binary input streams:
class FileSystem {
/* List all files in the directory */
List<String> listFiles(String path){...}
/* Check if the path is a directory */
boolean isDirectory(String path){...}
/* Get the size of the file in bytes */
int getFileSize(String path){...}
/* Open the file and return a binary stream */
InputStream openStream(String path){...}
}
Constraints:
"/".Example:
["DuplicateFileFinder", "findDuplicateFiles"] [[["/a/x.txt", "hello world"], ["/c/y.txt", "hello world"], ["/b/z.txt", "unique content"]]], []]
[null, [["/a/x.txt", "/c/y.txt"]]]
File PathContent"/a/x.txt""hello world""/c/y.txt""hello world""/b/z.txt""unique content" DuplicateFileFinder finder = new DuplicateFileFinder(fs); // Initialize with a file system containing the specified files. finder.findDuplicateFiles(); // Returns [["/a/x.txt", "/c/y.txt"]].