Easy
Given an array of strings, return another array containing all of its longest strings.
Example
For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].
Input/Output
-
[execution time limit] 0.5 seconds (c)
-
[input] array.string inputArray
A non-empty array.
Guaranteed constraints:
1 ≤ inputArray.length ≤ 10,
1 ≤ inputArray[i].length ≤ 10. -
[output] array.string
- Array of the longest strings, stored in the same order as in the inputArray.
[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 allLongestStrings(arr_string inputArray)
{
arr_string returnStrings;
int max=0, count=0;
for(int i=0;i<inputArray.size;i++)
max=strlen(inputArray.arr[i])>max?strlen(inputArray.arr[i]):max;
for(int i=0;i<inputArray.size;i++)
count+=strlen(inputArray.arr[i])==max;
returnStrings=alloc_arr_string(count);
for(int i=0;i<returnStrings.size;i++)
returnStrings.arr[i]=malloc((max+1)*sizeof(char));
for(int i=inputArray.size-1;i>=0;i--)
if(strlen(inputArray.arr[i])==max)
strcpy(returnStrings.arr[--count],inputArray.arr[i]);
return returnStrings;
}
728x90
'Codesignal' 카테고리의 다른 글
<Codesignal> isLucky (0) | 2020.04.05 |
---|---|
<Codesignal> commonCharacterCount (0) | 2020.04.05 |
<Codesignal> matrixElementsSum (0) | 2020.04.05 |
<Codesignal> almostIncreasingSequence (0) | 2020.04.05 |
<Codesignal> shapeArea (0) | 2020.04.05 |