-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path609. Find Duplicate File in System.java
29 lines (29 loc) · 1.32 KB
/
609. Find Duplicate File in System.java
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
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
Map<String, List<String>> contMap = new HashMap<>();
StringBuilder pathfile = new StringBuilder();
for (String pStr : paths) {
int i = 0;
pathfile.setLength(0);
while (pStr.charAt(i) != ' ') i++;
pathfile.append(pStr.substring(0,i)).append('/');
int pLen = ++i;
for (int j = i, k = 0; i < pStr.length(); i++)
if (pStr.charAt(i) == '(') {
pathfile.append(pStr.substring(j,i));
k = i + 1;
} else if (pStr.charAt(i) == ')') {
String cont = pStr.substring(k, i);
if (!contMap.containsKey(cont))
contMap.put(cont, new ArrayList<>());
contMap.get(cont).add(pathfile.toString());
j = i + 2;
pathfile.setLength(pLen);
}
}
List<List<String>> ans = new ArrayList<>();
for (List<String> v : contMap.values())
if (v.size() > 1) ans.add(v);
return ans;
}
}