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 that 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.
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)"
class Solution {
public:
vector<string> getFolderNames(vector<string>& names) {
unordered_map<string, int> files;
vector<string> res;
for(int i=0;i<names.size();i++) {
string cur=names[i];
if(files.find(cur)!=files.end()) {
int idx=files[cur]+1;
while(true) {
string tmp=cur+"("+to_string(idx)+")";
if(files.find(tmp)==files.end()) {
cur=tmp;
break;
}
else files[cur]++;
idx++;
}
}
files[cur]=0;
res.push_back(cur);
}
return res;
}
};