[LeetCode] Making File Names Unique

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 names[i].

Since two files cannot have the same name, if you enter a folder name which is 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<string> getFolderNames(vector<string>& names) {
unordered_map<string, int> namesMap;
for(int i = 0; i < names.size(); i++) {
if(!namesMap.count(names[i])) {
namesMap[names[i]]++;
} else {
for(int seq = namesMap[names[i]]; seq <= INT_MAX; seq++) {
string isExists = names[i] + "(" + to_string(seq) + ")";
if(namesMap.find(isExists) == namesMap.end()) {
namesMap[isExists]++;
namesMap[names[i]] = seq;
names[i] = isExists;
break;
}
}

}
}

return names;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/02/01/PS/LeetCode/making-file-names-unique/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.