Medium

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

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.string names

    Guaranteed constraints:
    5 ≤ names.length ≤ 1000,
    1 ≤ names[i].length ≤ 15.

  • [output] array.string

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
arr_string fileNaming(arr_string names)
{
	for(int i=0;i<names.size-1;i++)
	{
		int count=1;

		for(int j=i+1;j<names.size;j++)
			if(strcmp(names.arr[i],names.arr[j])==0)
			{
				sprintf(names.arr[j],"%s%c%d%c", names.arr[i],'(',count++,')');

				for(int k=0;k<j;k++)
					if(strcmp(names.arr[k],names.arr[j])==0)
						sprintf(names.arr[j],"%s%c%d%c", names.arr[i],'(',count++,')');
			}
	}

	return names;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> spiralNumbers  (0) 2020.04.06
<Codesignal> messageFromBinaryCode  (0) 2020.04.06
<Codesignal> digitsProduct  (0) 2020.04.06
<Codesignal> Different Squares  (0) 2020.04.06
<Codesignal> sumUpNumbers  (0) 2020.04.06

+ Recent posts