forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1487.java
41 lines (38 loc) · 1.41 KB
/
_1487.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
30
31
32
33
34
35
36
37
38
39
40
41
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class _1487 {
public static class Solution1 {
public String[] getFolderNames(String[] names) {
String[] result = new String[names.length];
Map<String, List<String>> map = new HashMap<>();
Set<String> used = new HashSet<>();
for (int i = 0; i < names.length; i++) {
if (!used.contains(names[i])) {
result[i] = names[i];
map.put(names[i], new ArrayList<>());
map.get(names[i]).add(names[i]);
used.add(names[i]);
} else {
List<String> list = map.get(names[i]);
int count = list.size();
String newName = names[i] + "(" + count + ")";
while (used.contains(newName)) {
count++;
newName = names[i] + "(" + count + ")";
}
result[i] = newName;
map.get(names[i]).add(newName);
used.add(newName);
map.put(newName, new ArrayList<>());
map.get(newName).add(newName);
}
}
return result;
}
}
}