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 names[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.
Example 1:
Input: names = ["pes","fifa","gta","pes(2019)"]
Output: ["pes","fifa","gta","pes(2019)"]
Explanation: Let's see how the file system creates folder names:
"pes" --> not assigned before, remains "pes"
"fifa" --> not assigned before, remains "fifa"
"gta" --> not assigned before, remains "gta"
"pes(2019)" --> not assigned before, remains "pes(2019)"
Example 2:
Input: names = ["gta","gta(1)","gta","avalon"]
Output: ["gta","gta(1)","gta(2)","avalon"]
Explanation: Let's see how the file system creates folder names:
"gta" --> not assigned before, remains "gta"
"gta(1)" --> not assigned before, remains "gta(1)"
"gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)"
"avalon" --> not assigned before, remains "avalon"
Example 3:
Input: names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
Output: ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)".
출처 : https://leetcode.com/problems/making-file-names-unique/description/
풀이
function getFolderNames(names: string[]): string[] {
const map = new Map();
for (const name of names) {
if (!map.has(name)) {
map.set(name, 1);
} else {
let count = map.get(name);
let newName = `${name}(${count})`;
while (map.has(newName)) {
count++;
newName = `${name}(${count})`;
}
map.set(name, count + 1);
map.set(newName, 1);
}
}
return [...map.keys()];
}
폴더 이름이 중복될 경우 새로운 이름을 생성하는 로직을 처리 하는 함수
예시 names = ["gta","gta(1)","gta","avalon"];
- map.keys():
- map은 자바스크립트의 Map 객체로, 키-값 쌍을 저장하는 자료구조
- map.keys()는 Map에 저장된 모든 키들을 이터레이터(Iterator) 형태로 반환
- 즉, map에서 모든 폴더 이름(키)만 추출하는 역할
- [...map.keys()]:
- ...(스프레드 연산자)는 이터레이터나 배열을 개별 요소로 펼치는 역할
- map.keys()는 이터레이터이므로, 스프레드 연산자를 사용하여 이 이터레이터의 모든 요소를 펼쳐서 배열로 변환
- 즉, [...map.keys()]는 map에 있는 모든 키들을 배열 형태로 만듦
- 최종 반환:
- 최종적으로, return 키워드는 이 배열을 반환
- 이 배열에는 map에 저장된 모든 폴더 이름들이 포함
- 즉, 중복 처리 후의 고유한 폴더 이름들이 배열로 반환
map = new Map();
map.set("gta", 2);
map.set("gta(1)", 1);
map.set("gta(2)", 1);
map.set("avalon", 1);
return [...map.keys()]; // ["gta", "gta(1)", "gta(2)", "avalon"]
이 배열을 입력으로 주었을 때, 함수는 다음과 같은 순서로 동작
- "gta"는 처음 등장한 이름이므로 map에 "gta"가 추가되고, 값은 1
- "gta(1)"도 처음 등장한 이름이므로 map에 "gta(1)"이 추가되고, 값은 1.
- 다시 "gta"가 등장했으므로, 기존 "gta"의 값(1)을 가져와 "gta(1)"이라는 새로운 이름을 시도.
- 그런데 "gta(1)"은 이미 존재하므로, 값을 2로 증가시키고 "gta(2)"라는 새로운 이름을 생성하여 추가
- "avalon"은 처음 등장했으므로 바로 map에 추가
최종 값은 ["gta", "gta(1)", "gta(2)", "avalon"]
'LeetCode' 카테고리의 다른 글
| 736. Parse Lisp Expression / TypeScript (4) | 2024.09.24 |
|---|---|
| 2193. Minimum Number of Moves to Make Palindrome / TypeScipt (1) | 2024.09.23 |
| 2762. Continuous Subarrays / TypeScript (1) | 2024.09.16 |
| 452. Minimum Number of Arrows to Burst Balloons / TypeScript (1) | 2024.09.12 |
| 1048. Longest String Chain / TypeScript (2) | 2024.09.10 |