Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d9f14da

Browse files
committed
Add solution #1487
1 parent 74cdddd commit d9f14da

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,325 LeetCode solutions in JavaScript
1+
# 1,326 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1136,6 +1136,7 @@
11361136
1482|[Minimum Number of Days to Make m Bouquets](./solutions/1482-minimum-number-of-days-to-make-m-bouquets.js)|Medium|
11371137
1483|[Kth Ancestor of a Tree Node](./solutions/1483-kth-ancestor-of-a-tree-node.js)|Hard|
11381138
1486|[XOR Operation in an Array](./solutions/1486-xor-operation-in-an-array.js)|Easy|
1139+
1487|[Making File Names Unique](./solutions/1487-making-file-names-unique.js)|Medium|
11391140
1491|[Average Salary Excluding the Minimum and Maximum Salary](./solutions/1491-average-salary-excluding-the-minimum-and-maximum-salary.js)|Easy|
11401141
1492|[The kth Factor of n](./solutions/1492-the-kth-factor-of-n.js)|Medium|
11411142
1493|[Longest Subarray of 1's After Deleting One Element](./solutions/1493-longest-subarray-of-1s-after-deleting-one-element.js)|Medium|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1487. Making File Names Unique
3+
* https://leetcode.com/problems/making-file-names-unique/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of strings names of size n. You will create n folders in your file system such
7+
* that, at the ith minute, you will create a folder with the name names[i].
8+
*
9+
* Since two files cannot have the same name, if you enter a folder name that was previously used,
10+
* the system will have a suffix addition to its name in the form of (k), where, k is the smallest
11+
* positive integer such that the obtained name remains unique.
12+
*
13+
* Return an array of strings of length n where ans[i] is the actual name the system will assign
14+
* to the ith folder when you create it.
15+
*/
16+
17+
/**
18+
* @param {string[]} names
19+
* @return {string[]}
20+
*/
21+
var getFolderNames = function(names) {
22+
const map = new Map();
23+
const result = [];
24+
25+
for (const name of names) {
26+
if (!map.has(name)) {
27+
result.push(name);
28+
map.set(name, 1);
29+
} else {
30+
let suffix = map.get(name);
31+
let newName = `${name}(${suffix})`;
32+
while (map.has(newName)) {
33+
suffix++;
34+
newName = `${name}(${suffix})`;
35+
}
36+
result.push(newName);
37+
map.set(name, suffix + 1);
38+
map.set(newName, 1);
39+
}
40+
}
41+
42+
return result;
43+
};

0 commit comments

Comments
 (0)