Advertisement

Find Duplicate File in System - LeetCode 609 Solution

Find Duplicate File in System - Complete Solution Guide

Find Duplicate File in System is LeetCode problem 609, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a list paths of directory info, including the directory path, and all the files with contents in this directory, return all the duplicate files in the file system in terms of their paths . You may return the answer in any order . A group of duplicate files consists of at least two files that have the same content. A single directory info string in the input list has the following format: "root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)" It means there are n f

Detailed Explanation

The problem requires identifying duplicate files within a file system, given directory information as a list of strings. Each string represents a directory path followed by space-separated file entries in the format 'filename(content)'. The goal is to return a list of lists, where each inner list contains the full paths of files sharing the same content. A group of duplicate files must have at least two files.

Solution Approach

The provided solutions employ a hash table (dictionary/map) to group files based on their content. The algorithm iterates through the input `paths` list, parses each string to extract the directory path, filename, and content. The content is used as the key in the hash table, and the corresponding value is a list of full file paths that share that content. Finally, the algorithm iterates through the hash table and returns a list of lists containing file paths for any content that has more than one file associated with it (duplicates).

Step-by-Step Algorithm

  1. Step 1: Initialize a hash table (dictionary/map) `content_map` to store file content as keys and a list of corresponding file paths as values.
  2. Step 2: Iterate through each string `path_info` in the input list `paths`.
  3. Step 3: Split each `path_info` string by spaces to separate the directory path from the file entries.
  4. Step 4: Extract the directory path from the first element of the split string.
  5. Step 5: Iterate through the file entries (remaining elements after the directory path).
  6. Step 6: For each file entry, extract the filename and content by splitting the string at the '(' character or using `rpartition`.
  7. Step 7: Construct the full file path by concatenating the directory path, '/', and the filename.
  8. Step 8: Add the full file path to the list associated with the content in the `content_map`. If the content doesn't exist as a key, create a new entry with the content as the key and a new list containing the file path as the value.
  9. Step 9: After processing all input strings, iterate through the `content_map`.
  10. Step 10: For each entry in the `content_map`, if the list of file paths has more than one element, it means there are duplicate files with the same content. Add this list to the result list.
  11. Step 11: Return the result list, which contains lists of duplicate file paths.
  12. Step 12 (C Specific): Handle memory allocation and deallocation for each string created to avoid memory leaks.

Key Insights

  • Insight 1: The core task is to group files by their content. Therefore, a hash table (dictionary or map) is the most suitable data structure to store content as keys and a list of file paths as values.
  • Insight 2: The input string needs to be parsed effectively to extract the directory path, filename, and content. String manipulation techniques like splitting and finding substrings are essential.
  • Insight 3: The problem's constraints specify the formats and ranges of input data, guiding the choice of appropriate data structures and algorithms. Handling memory allocation and deallocation properly is critical in languages like C to avoid memory leaks.

Complexity Analysis

Time Complexity: O(N*L)

Space Complexity: O(N*L)

Topics

This problem involves: Array, Hash Table, String.

Companies

Asked at: Applied Intuition, Dropbox, Turing.