File Naming
Description
You are given an array of desired filenames in the order of their creation. Since two files cannot have equal names, the one which comes later will have an addition to its name in a form of (k)
, where k
is the smallest positive integer such that the obtained name is not used yet.
Return an array of names that will be given to the files.
Example
For names = ["doc", "doc", "image", "doc(1)", "doc"]
, the output should be
fileNaming(names) = ["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"]
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] array.string names
Guaranteed constraints:
5 ≤ names.length ≤ 15
,
1 ≤ names[i].length ≤ 15
.
- [output] array.string
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
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
function fileNaming(names) {
var n = [];
for(var i=0; i < names.length; i++) {
var max = 0;
var m = [];
var f = n.filter(j=>{
var reg = new RegExp('^(?:' + names[i].replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + '(?:\\((\\d+)\\))?)$');
if(reg.test(j)) {
if(reg.exec(j)[1] ==undefined )
m[0] =true;
else {
m[Number(reg.exec(j)[1])] = true;
}
}
return reg.test(j);
});
for(var k = 0; k < m.length; k++) {
if(!m[k])
break;
}
if(f.length){
names[i] = names[i]+ (k ? ('('+ (k)+')') : '');
}
n.push(names[i])
}
return n;
}