-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakingFileNamesUnique.java
More file actions
26 lines (25 loc) · 1.17 KB
/
MakingFileNamesUnique.java
File metadata and controls
26 lines (25 loc) · 1.17 KB
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
/**
* 1487. Making File Names Unique
*
* Given an array of strings names of size n.
* You will create n folders in your file system such that, at the ith minute, you will create a folder with the name theNames[i].
* Since two files cannot have the same name, if you enter a folder name that was previously used,
* the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique.
* Return an array of strings of length n where ans[i] is the actual name the system will assign to the ith folder when you create it.
*/
class MakingFileNamesUnique {
public String[] getFolderNames(String[] theNames) {
Map<String, Integer> m = new HashMap<>();
for (int i = 0; i < theNames.length; i++) {
if (m.containsKey(theNames[i])) {
int k = m.get(theNames[i]);
while (m.containsKey(theNames[i] + "(" + k + ")"))
k++;
m.put(theNames[i], k + 1);
theNames[i] = theNames[i] + "(" + k + ")";
}
m.put(theNames[i], 1);
}
return theNames;
}
}