[LeetCode] Find Duplicate File in System

609. Find Duplicate File in System

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 files (f1.txt, f2.txt … fn.txt) with content (f1_content, f2_content … fn_content) respectively in the 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 groups 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”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
vector<string> split(string& s) {
stringstream ss(s);
istream_iterator<string> begin(ss);
istream_iterator<string> end;
vector<string> res(begin, end);
copy(res.begin(), res.end(), ostream_iterator<string>(cout, "\n"));
return res;
}
unordered_map<string, vector<string>> sort(string& s) {
vector<string> str = split(s);
string path = str.front() + "/";
unordered_map<string, vector<string>> res;
for(int i = 1; i < str.size(); i++) {
auto p = divide(str[i]);
res[p.first].push_back(path+p.second);
}

return res;
}
pair<string, string> divide(string& s) {
int pos = s.find('(');
string innerText = s.substr(pos);
string file = s.substr(0, pos);
return {innerText, file};
}
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
unordered_map<string, vector<string>> m;
for(auto& p : paths) {
unordered_map<string, vector<string>> res = sort(p);
for(auto& e : res) {
m[e.first].insert(m[e.first].end(), begin(e.second), end(e.second));
}
}
vector<vector<string>> res;
for(auto& e : m) {
if(e.second.size() != 1)
res.push_back(e.second);
}

return res;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
unordered_map<string, vector<string>> m;
vector<vector<string>> res;
const char SLASH = '/';
const char SPACE = ' ';
for(auto& p : paths) {
stringstream ss(p);
string path;
string file;
getline(ss, path, SPACE);
while(getline(ss, file, SPACE)) {
m[file.substr(file.find('('))].push_back(path + SLASH + file.substr(0, file.find('(')));
}
}
for(auto& e : m) {
if(e.second.size() != 1)
res.push_back(e.second);
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/15/PS/LeetCode/find-duplicate-file-in-system/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.