알고리즘/codefights

57>fileNaming

Diademata 2018. 4. 13. 00:25
반응형

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)"]. 


code>>


std::vector<std::string> fileNaming(std::vector<std::string> names) {

std::set<std::string> _list;

for (int i = 0; i < names.size(); i++)

{

if (_list.count(names[i]) > 0)

{

int num = 1;

while (_list.count(names[i] + "(" + std::to_string(num) + ")") > 0)

num++;

names[i] += "(" + std::to_string(num) + ")";

}

_list.insert(names[i]);

}

return names;

}

반응형