LeetCode Find Duplicate File in System
原题链接在这里: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.txt, f2.txt ... fn.txt with content f1_content, f2_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:
- No order is required for the final output.
- 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].
- The number of files given is in the range of [1,20000].
- You may assume no files or directories share the same name in the same directory.
- 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:
- Imagine you are given a real file system, how will you search files? DFS or BFS?
- If the file content is very large (GB level), how will you modify your solution?
- If you can only read the file by 1kb each time, how will you modify your solution?
- 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?
- 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:
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
List<List<String>> res = new ArrayList<List<String>>();
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
for(String path : paths){
String [] pathArr = path.split("\\s+");
for(int i = 1; i<pathArr.length; i++){
String content = pathArr[i].substring(pathArr[i].indexOf("("));
String fileName = pathArr[i].substring(0, pathArr[i].indexOf("("));
List<String> list = hm.getOrDefault(content, new ArrayList<String>());
list.add(pathArr[0] + "/" + fileName);
hm.put(content, list);
}
}
for(String key : hm.keySet()){
if(hm.get(key).size() > 1){
res.add(hm.get(key));
}
}
return res;
}
}
LeetCode Find Duplicate File in System的更多相关文章
- [LeetCode] Find Duplicate File in System 在系统中寻找重复文件
Given a list of directory info including directory path, and all the files with contents in this dir ...
- LC 609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- 【LeetCode】609. Find Duplicate File in System 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】609. Find Duplicate File in System
题目如下: Given a list of directory info including directory path, and all the files with contents in th ...
- [Swift]LeetCode609. 在系统中查找重复文件 | Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- [leetcode-609-Find Duplicate File in System]
https://discuss.leetcode.com/topic/91430/c-clean-solution-answers-to-follow-upGiven a list of direct ...
- 609. Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- Find Duplicate File in System
Given a list of directory info including directory path, and all the files with contents in this dir ...
- HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)
Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...
随机推荐
- 2062326 齐力锋 实验三《敏捷开发与XP实践》实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程: 程序设计与数据结构 班级: 1623 姓名: 齐力锋 学号: 20162326 成绩: 指导教师: 娄嘉鹏/王志强 实验日期: 2017年5月1 ...
- iOS设计模式探索
常用的 23 种设计模式 不管是 .NET 中的 C# 语言,还是 Java.VB.NET.C++ 或 Objective-C 语言,面向对象语言在设计模式的层面上都是相通的,只不过在设计模式的具体实 ...
- Mysql数据库导出sql脚本
1. 运行环境Centos mysqldump -h localhost -u root -p etv > ./etv.sql etv 是要导出的数据库名 > 设置导出的路径和文件名
- dubbo学习小结
dubbo学习小结 参考: https://blog.csdn.net/paul_wei2008/article/details/19355681 https://blog.csdn.net/liwe ...
- HUE中Oozie执行Sqoop
Oozie执行Sqoop,传入参数(注意,在使用--query时,参数中一定不要带有空格,否则出错)1. 新建一个workflow 2. 拖入一个sqoop 3. sqoop抽取命令如下(建议先在命令 ...
- CodeChef CHEFSOC2 Chef and Big Soccer 水dp
Chef and Big Soccer Problem code: CHEFSOC2 Tweet ALL SUBMISSIONS All submissions for this prob ...
- vue 的小秘密
1.组件可以通过$refs调用其方法. 2.组件上也可用v-model. <input v-model="something"> == 同等 <input v-b ...
- python学习笔记(arange函数与linspace函数)
上一篇提及到matplotlib模块.其中会涉及到numpy模块科学计数 这里总结两个数组生成函数 arange 与 linspace: #!/usr/bin/env python # -*- cod ...
- (CSharp)克隆控件事件
// https://stackoverflow.com/questions/6055038/how-to-clone-control-event-handlers-at-run-time // &q ...
- Mysql的锁机制——待写
发酵独守空房飞杀戮空间爱的色放