-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_duplicate_file_in_system.cpp
51 lines (45 loc) · 1.39 KB
/
find_duplicate_file_in_system.cpp
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
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
using namespace std;
class Solution {
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
map<string, vector<string>> path_map;
for (auto &path: paths) {
read_content(path, path_map);
}
vector<vector<string>> ans;
for (auto iter = path_map.begin(); iter != path_map.end(); iter++)
if (iter -> second.size() > 1)
ans.push_back(iter -> second);
return ans;
}
private:
void read_content(string& path, map<string, vector<string>> &path_map) {
string dirname, file_content, filename, content;
istringstream is(path);
is >> dirname;
while (is >> file_content) {
auto pos = file_content.find('(');
filename = dirname + "/" + file_content.substr(0, pos);
content = file_content.substr(pos + 1, string::npos);
path_map[content].push_back(filename);
}
}
};
int main(int argc, char const *argv[])
{
Solution s;
vector<string> paths = {"root/a 1.txt(abcd) 2.txt(efsfgh)","root/c 3.txt(abdfcd)","root/c/d 4.txt(efggdfh)"};
for (auto &path_v: s.findDuplicate(paths)) {
cout << "group:\n\t";
for (auto &v: path_v) {
cout << v << '\t';
}
cout << '\n';
}
return 0;
}