LeetCode Find Duplicate File in System

本文介绍了一种使用HashMap解决LeetCode上查找系统中重复文件的问题的方法。通过解析输入字符串,利用文件内容作为键,文件路径作为值,找出所有具有相同内容的文件组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接在这里:https://leetcode.com/problems/find-duplicate-file-in-system/description/

题目:

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths.

A group of duplicate files consists of at least two files that have exactly 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 files (f1.txtf2.txt ... fn.txt with content f1_contentf2_content ... fn_content, respectively) in directory root/d1/d2/.../dm. Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.

The output is a list of group of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:

"directory_path/file_name.txt"

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

Note:

  1. No order is required for the final output.
  2. You may assume the directory name, file name and file content only has letters and digits, and the length of file content is in the range of [1,50].
  3. The number of files given is in the range of [1,20000].
  4. You may assume no files or directories share the same name in the same directory.
  5. You may assume each given directory info represents a unique directory. Directory path and file info are separated by a single blank space.

Follow-up beyond contest:

  1. Imagine you are given a real file system, how will you search files? DFS or BFS?
  2. If the file content is very large (GB level), how will you modify your solution?
  3. If you can only read the file by 1kb each time, how will you modify your solution?
  4. What is the time complexity of your modified solution? What is the most time-consuming part and memory consuming part of it? How to optimize?
  5. How to make sure the duplicated files you find are not false positive?

题解:

用HashMap<String, List<String>> hm 来保存每个file 的content 和对应的path集合.

每个input string 按照 path fileName1(content1) fileName2(content2) 格式输入. 所以先按照空格断开,后面的都是文件名加上内容,再用"("断开提取内容.

最后看内容对应文件数大于1的就是有duplicate.

Time Complexity: O(paths.length * x). x为input string的平均长度.

Space:O(paths.length * x). hm size.

AC Java:

 1 class Solution {
 2     public List<List<String>> findDuplicate(String[] paths) {
 3         List<List<String>> res = new ArrayList<List<String>>();
 4         HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
 5         
 6         for(String path : paths){
 7             String [] pathArr = path.split("\\s+");
 8             for(int i = 1; i<pathArr.length; i++){
 9                 String content = pathArr[i].substring(pathArr[i].indexOf("("));
10                 String fileName = pathArr[i].substring(0, pathArr[i].indexOf("("));
11                 List<String> list = hm.getOrDefault(content, new ArrayList<String>());
12                 list.add(pathArr[0] + "/" + fileName);
13                 hm.put(content, list);
14             }
15         }
16         
17         for(String key : hm.keySet()){
18             if(hm.get(key).size() > 1){
19                 res.add(hm.get(key));
20             }
21         }
22         
23         return res;
24     }
25 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7720423.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值