Sorting Array of Strings || Hacker Rank

 Question 18 || Hacker Rank

Sorting Array of Strings || Hacker Rank

Sorting Array of Strings


To sort a given cluster of strings into lexicographically expanding request or into a request wherein the string with the least length shows up initial, an arranging capability with a banner demonstrating the kind of examination methodology can be composed. The weakness with doing so is changing the capability for each new correlation technique.

 

A superior execution is composing an arranging capability that acknowledges a pointer to the capability that looks at each sets of strings. Doing this will mean just passing a pointer to the arranging capability with each new correlation system.

 

Given a variety of strings, you really want to carry out a capability which sorts the strings as per an examination capability, i.e., you really want to execute the capability of the function:

 

void string_sort(const char **arr,const int cnt, int (*cmp_func)(const char* a, const char* b)){
    
}

 

The contentions passed to this capability are:

Sorting Array of Strings

You additionally need to execute the accompanying four string correlation capabilities:

Sorting Array of Strings

Input Organization

 You simply have to finish the capability string\_sort and execute the four string examination capabilities.

 

Imperatives

Sorting Array of Strings

Yield Arrangement

 The locked code-stub will really take a look at the rationale of your code. The result comprises of the strings arranged by the four comparsion capabilities in the request referenced in the issue explanation.

 

Test Information 0

4

wkue

qoi

sbv

fekls

 

Test Result 0

fekls

qoi

sbv

wkue

 

wkue

sbv

qoi

fekls

 

qoi

sbv

wkue

fekls

 

qoi

sbv

wkue

fekls


#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 sort_by_number_of_distinct_characters(const char* a, const char* b) 
{
    int count_a = 0, count_b = 0;
    int freq_a[26] = {0}, freq_b[26] = {0};

    for (int i = 0; i < strlen(a); i++) 
    {
        if (freq_a[a[i] - 'a'] == 0
        {
            count_a++;
            freq_a[a[i] - 'a'] = 1;
        }
    }

    for (int i = 0; i < strlen(b); i++) 
    {
        if (freq_b[b[i] - 'a'] == 0
        {
            count_b++;
            freq_b[b[i] - 'a'] = 1;
        }
    }

    if (count_a == count_b) 
    {
        return strcmp(a, b);
    } 
    else 
    {
        return (count_a - count_b);
    }
}

int sort_by_length(const char* a, const char* b) 
{
    int len_a = strlen(a);
    int len_b = strlen(b);

    if (len_a == len_b) 
    {
        return strcmp(a, b);
    } 
    else 
    {
        return (len_a - len_b);
    }
}

void string_sort(char** arr, const int len, int (*cmp_func)(const char* a, const char* b))
{
    for (int i = 0; i < len; i++) 
    {
        for (int j = i + 1; j < len; j++) 
        {
            if (cmp_func(arr[i], arr[j]) > 0
            {
                char* temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}



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");






Output:

Sorting Array of Strings


Post a Comment

0 Comments