LeetCode 609: Find Duplicate File in System Solution

Master LeetCode problem 609 (Find Duplicate File in System), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

609. Find Duplicate File in System

Problem Explanation

Explanation:

To solve this problem, we can use a hashmap where the key is the file content and the value is a list of file paths with that content. We iterate through each directory info string, extract the file content and file paths, and store them in the hashmap. Finally, we collect all the values from the hashmap where the list size is greater than 1, as these are the duplicate files.

  • Time Complexity: O(n * k) where n is the number of directory info strings and k is the average length of a directory info string.
  • Space Complexity: O(n * k) for the hashmap.

:

Solution Code

import java.util.*;

class Solution {
    public List<List<String>> findDuplicate(String[] paths) {
        Map<String, List<String>> map = new HashMap<>();
        
        for (String path : paths) {
            String[] parts = path.split(" ");
            String directory = parts[0];
            
            for (int i = 1; i < parts.length; i++) {
                String[] fileInfo = parts[i].split("\\(");
                String fileName = fileInfo[0];
                String content = fileInfo[1].substring(0, fileInfo[1].length() - 1);
                String fullPath = directory + "/" + fileName;
                
                map.putIfAbsent(content, new ArrayList<>());
                map.get(content).add(fullPath);
            }
        }
        
        List<List<String>> result = new ArrayList<>();
        for (List<String> files : map.values()) {
            if (files.size() > 1) {
                result.add(files);
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 609 (Find Duplicate File in System)?

This page provides optimized solutions for LeetCode problem 609 (Find Duplicate File in System) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 609 (Find Duplicate File in System)?

The time complexity for LeetCode 609 (Find Duplicate File in System) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 609 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 609 in Java, C++, or Python.

Back to LeetCode Solutions