609. Find Duplicate File in System(LeetCode)

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?

     1 unordered_map<string, vector<string>> files;
     2         vector<vector<string>> result;
     3
     4         for (auto path : paths) {
     5             stringstream ss(path);
     6             string root;
     7             string s;
     8             getline(ss, root, ‘ ‘);
     9             while (getline(ss, s, ‘ ‘)) {
    10                 string fileName = root + ‘/‘ + s.substr(0, s.find(‘(‘));
    11                 string fileContent = s.substr(s.find(‘(‘) + 1, s.find(‘)‘) - s.find(‘(‘) - 1);
    12                 files[fileContent].push_back(fileName);
    13             }
    14         }
    15
    16         for (auto file : files) {
    17             if (file.second.size() > 1)
    18                 result.push_back(file.second);
    19         }
    20
    21         return result;
时间: 2024-10-13 04:27:17

609. Find Duplicate File in System(LeetCode)的相关文章

609. Find Duplicate File in System

寻找重复文件的路径集合 /* 没有什么特别的技巧,就是用map存储相同内容的路径,核心就是getOrDefault()方法 注意步骤,想清楚思路 */ //记录结果 Map<String,ArrayList> map = new HashMap<>(); for (String path : paths) { //把根路径和各个文件分开 String[] files = path.split(" "); //遍历每个文件,有两个任务,一是存下改文件内容,二是把该

POJ 1018 Communication System (动态规划)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22500   Accepted: 8008 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

JAVA File类 分析(三)

前面两篇与大家一起研究了unix下的文件系统,本篇将和大家一起分析 文件的属性和文件夹. ok,废话不说,先来段代码 #include <stdio.h> #include <sys/types.h> #include <dirent.h> void do_ls(char[]); void main(int ac,char *av[]){ if(ac==1) do_ls("."); else{ while(--ac){ printf("%s

system(“pause”)和getchar()

大家都知道system("PAUSE")可以让C程序在运行结束之前暂停运行.用system("PAUSE")可以解决运行程序一闪而过,看不到输出结果的问题.有程序员会用system("PAUSE")只是为了能够让程序暂停,以便看到运行的结果.这就好比用大炮打蚊子杀鸡用牛刀.那么system("PAUSE")语句究竟怎么运行呢? 下面是关于system("PAUSE")的一些观点: 1.平台相关性.syste

关于system(”pause“);的作用和意义

注意:不要再return 的语句之后加,那样就执行不到了. system() 是调用系统命令:pause 暂停命令: 如果加有  system(”pause“): 这样在运行到此处时,会显示“Press any key to continue . . .” 也就是 “按任意键继续...”: 在VC 6.0下,要添加如下的头文件! #include <stdlib.h> 在c++  程序中用#include<iostream>也可以

119. Pascal&#39;s Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

hdu 1690 Bus System(Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Problem Description Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it's

HDU-4081 Qin Shi Huang&#39;s National Road System(最小生成树)

今天比赛AC的一道最小生成树的题目 , 学到了不少东西 . 最小生成树的模板很简单,最简洁好写的还是lrj紫书上的代码 .利用并查集加速算法 . 该题的不同之处在于它选择任意一条路修成"魔法"道路 , 然后其他路的权值之和还要是最小的一棵次小生成树,并且求魔法道路两端点值之和除以其他路径长之和的最大值 . 显然该题的难点在于枚举两个端点之后怎么快速的求出次小生成树权值之和 .  枚举两个端点之后的复杂度已经是O(n^2),所以要想出一个快速的方法 . 受紫书上例题uva 1151 (传

HDU 4081 Qin Shi Huang&#39;s National Road System(prim)

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5153    Accepted Submission(s): 1795 Problem Description During the Warring States Period of ancient China(4