-
Notifications
You must be signed in to change notification settings - Fork 6
/
MakingFileNamesUnique.java
34 lines (32 loc) · 1.06 KB
/
MakingFileNamesUnique.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
package com.shivaprasad.march.day66;
import java.util.HashMap;
import java.util.Map;
public class MakingFileNamesUnique {
public static void main(String[] args) {
String[] res = getFolderNames(new String[]{"onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"});
for(String s:res)
{
System.out.print(s+" ");
}
}
static String[] getFolderNames(String[] names) {
Map<String, Integer> map = new HashMap<>();
for (int i=0; i<names.length; i++) {
String current = names[i];
while (map.containsKey(current)) {
int count = map.get(names[i]);
count += 1;
map.put(names[i], count);
StringBuilder sb = new StringBuilder();
sb.append(names[i]);
sb.append("(");
sb.append(count);
sb.append(")");
current = sb.toString();
}
map.put(current, 0);
names[i] = current;
}
return names;
}
}