-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathFind_Duplicate_File_in_System.cpp
45 lines (38 loc) · 1.33 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
class Solution {
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
vector<vector<string>> result;
unordered_map<string, vector<string>> duplicateMap;
for(string& path: paths) {
int i = 0, m = (int)path.length();
while(i < m and path[i] != ' ') {
i++;
}
string pathPrefix = path.substr(0, i);
while(i < m) {
// skip space
while(i < m and path[i] == ' ') { i++; }
string fileName;
while(i < m and path[i] != '(') {
fileName += path[i];
i++;
}
i++;
string fileContent;
while(i < m and path[i] != ')') {
fileContent += path[i];
i++;
}
i++;
string file = pathPrefix + "/" + fileName;
duplicateMap[fileContent].push_back(file);
}
}
for(auto iter = duplicateMap.begin(); iter != duplicateMap.end(); iter++) {
if(iter->second.size() > 1) {
result.push_back(iter->second);
}
}
return result;
}
};