Skip to content

Commit e778d03

Browse files
add 1487
1 parent 75e4e32 commit e778d03

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1487|[Making File Names Unique](https://leetcode.com/problems/making-file-names-unique/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1487.java) | |Medium|HashTable, String|
1112
|1486|[XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1486.java) | |Medium|Array, Bit Manipulation|
1213
|1481|[Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1481.java) | |Medium|Array, Sort|
1314
|1480|[Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1480.java) | |Easy|Array|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
public class _1487 {
11+
public static class Solution1 {
12+
public String[] getFolderNames(String[] names) {
13+
String[] result = new String[names.length];
14+
Map<String, List<String>> map = new HashMap<>();
15+
Set<String> used = new HashSet<>();
16+
for (int i = 0; i < names.length; i++) {
17+
if (!used.contains(names[i])) {
18+
result[i] = names[i];
19+
map.put(names[i], new ArrayList<>());
20+
map.get(names[i]).add(names[i]);
21+
used.add(names[i]);
22+
} else {
23+
List<String> list = map.get(names[i]);
24+
int count = list.size();
25+
String newName = names[i] + "(" + count + ")";
26+
while (used.contains(newName)) {
27+
count++;
28+
newName = names[i] + "(" + count + ")";
29+
}
30+
result[i] = newName;
31+
map.get(names[i]).add(newName);
32+
used.add(newName);
33+
34+
map.put(newName, new ArrayList<>());
35+
map.get(newName).add(newName);
36+
}
37+
}
38+
return result;
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1487;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1487Test {
10+
private static _1487.Solution1 solution1;
11+
private static String[] names;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1487.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
names = new String[]{"pes", "fifa", "gta", "pes(2019)"};
21+
assertArrayEquals(new String[]{"pes", "fifa", "gta", "pes(2019)"}, solution1.getFolderNames(names));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)