-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0609-find-duplicate-file-in-system.cpp
More file actions
52 lines (46 loc) · 1.64 KB
/
Copy path0609-find-duplicate-file-in-system.cpp
File metadata and controls
52 lines (46 loc) · 1.64 KB
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
52
class Solution {
public:
pair<string, vector<pair<string, string>>> enumerate_path(string s) {
pair<string, vector<pair<string, string>>> res; //pair is (content, number.txt)
int index = 0;
while (index < s.size() && s[index] != ' ') {
index++;
}
vector<pair<string, string>> files;
res.first = s.substr(0, index);
while (index < s.size()) {
string current;
string num_current;
if (s[index] == ' ') index++;
while (index < s.size() && s[index] != '(') {
num_current += s[index];
index++;
}
index++;
while (index < s.size() && s[index] != ')') {
current += s[index];
index++;
}
index++;
files.emplace_back(current, num_current);
}
res.second = files;
return res;
}
vector<vector<string>> findDuplicate(vector<string>& paths) {
unordered_map<string, vector<string>> data_to_path;
vector<vector<string>> res;
for (auto& path: paths) {
pair<string, vector<pair<string, string>>>split_path = enumerate_path(path);
for (int index = 0; index < split_path.second.size(); ++index) {
string path_name = split_path.first + "/" + split_path.second[index].second;
data_to_path[split_path.second[index].first].push_back(path_name);
}
}
for (auto& [key, value]: data_to_path) {
if (value.size() <= 1) continue;
res.push_back(value);
}
return res;
}
};