Sorting Array of Strings - Hacker Rank Solution
Sorting Array of Strings - Hacker Rank Solution |
Problem
To sort a given array of strings into lexicographically increasing order or into an order in which the string with the lowest length appears first, a sorting function with a flag indicating the type of comparison strategy can be written. The disadvantage with doing so is having to rewrite the function for every new comparison strategy.
A better implementation would be to write a sorting function that accepts a pointer to the function that compares each pair of strings. Doing this will mean only passing a pointer to the sorting function with every new comparison strategy.
Given an array of strings, you need to implement a
string_sort function which sorts the strings according to
a comparison function, i.e, you need to implement the function :
void string_sort(const char **arr,const int cnt, int (*cmp_func)(const char* a, const char* b)){ }
The arguments passed to this function are:
- an array of strings : arr
- length of string array: count
- pointer to the string comparison function:cmp_func
- int lexicographic_sort(const char*, const char*) to sort the strings in lexicographically non-decreasing order.
- int lexicographic_sort_reverse(const char*, const char*) to sort the strings in lexicographically non-increasing order.
- int sort_by_number_of_distinct_characters(const char*, const char*) to sort the strings in non-decreasing order of the number of distinct characters present in them. If two strings have the same number of distinct characters present in them, then the lexicographically smaller string should appear first.
- int sort_by_length(const char* a, const char* b) to sort the strings in non-decreasing order of their lengths. If two strings have the same length, then the lexicographically smaller string should appear first.
Input Format :
Constraints :
- 1<= No. of Strings <= 50
- 1<= Total length of all the Strings <=2500
- You have to write your own sorting function and you cannot use the inbuilt qsort function
- The strings consists of lower-case English Alphabets only.
Output Format :
Sample Input 0 :
4
wkue
qoi
sbv
fekls
Sample Output 0 :
fekls qoi sbv wkue wkue sbv qoi fekls qoi sbv wkue fekls qoi sbv wkue fekls
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | //Sorting Array of Strings - Hacker Rank Solution #include <stdio.h> #include <stdlib.h> #include <string.h> int lexicographic_sort(const char* a, const char* b) { return strcmp(a, b); } int lexicographic_sort_reverse(const char* a, const char* b) { return strcmp(b, a); } int characters_count(const char *s) { int n = 0; int count[128] = {0}; if (NULL == s) { return -1; } while(*s != '\0') { if (!count[*s]) { count[*s]++; n++; } s++; } return n; } int sort_by_number_of_distinct_characters(const char* a, const char* b) { int con = characters_count(a) - characters_count(b); return (con ? con : lexicographic_sort(a, b)); } int sort_by_length(const char* a, const char* b) { int len = strlen(a) - strlen(b); return (len ? len : lexicographic_sort(a, b)); } void string_sort(char** arr,const int len,int (*cmp_func)(const char* a, const char* b)) { int mid = len / 2; int con = 0; while(!con) { con = 1; for(int i = 0; i < len - 1; i++) { if(cmp_func(arr[i], arr[i + 1]) > 0) { char *temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; con = 0; } } } } int main() { int n; scanf("%d", &n); char** arr; arr = (char**)malloc(n * sizeof(char*)); for(int i = 0; i < n; i++) { *(arr + i) = malloc(1024 * sizeof(char)); scanf("%s", *(arr + i)); *(arr + i) = realloc(*(arr + i), strlen(*(arr + i)) + 1); } string_sort(arr, n, lexicographic_sort); for(int i = 0; i < n; i++) printf("%s\n", arr[i]); printf("\n"); string_sort(arr, n, lexicographic_sort_reverse); for(int i = 0; i < n; i++) printf("%s\n", arr[i]); printf("\n"); string_sort(arr, n, sort_by_length); for(int i = 0; i < n; i++) printf("%s\n", arr[i]); printf("\n"); string_sort(arr, n, sort_by_number_of_distinct_characters); for(int i = 0; i < n; i++) printf("%s\n", arr[i]); printf("\n"); } |
Disclaimer :-
the above whole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
olution.c: In function ‘characters_count’:
Solution.c:25:19: warning: array subscript has type ‘char’ [-Wchar-subscripts]
if (!count[*s])
^
Solution.c:27:18: warning: array subscript has type ‘char’ [-Wchar-subscripts]
count[*s]++;
^
Solution.c: In function ‘string_sort’:
Solution.c:49:7: warning: unused variable ‘mid’ [-Wunused-variable]
int mid = len / 2;
^~~
Solution.c: At top level:
Solution.c:107:5: error: redefinition of ‘main’
int main()
^~~~
Solution.c:68:5: note: previous definition of ‘main’ was here
int main()
^~~~
Exit Status
1