Count of strings to be concatenated with a character having frequency greater than sum of others


Our main aim here is to determine the most strings that are capable of being concatenated to ensure that just one letter has a frequency that exceeds the total of all the other characters, provided an array called arr[] containing M strings.

Before going further, let's understand some basic concepts of array and string.

The array is nothing but a group of identically data-typed elements held in consecutive memory sections.

The array in the C programming language has a fixed size, which means that once the size is specified, it cannot be changed; you cannot shrink or extend it.

Let's now examine what a string is. A string is a grouping of characters that terminates with the null character "\0" in C programming language. Characters from the C String are kept in a character array. A C string contradicts a character array in the very reason that it ends with the distinctive character null unlike the character array.

Problem Statement

Implement a program to determine the count of strings to be concatenated with a character having frequency greater than sum of others.

Sample Example 1

Let us take the input array 
arr[]: {“xyz", “yyyyx", “q”}
Output obtained is: 3

Explanation

Here the frequency of the element "x" is 2.

The frequency of the element "y" is 5 and the frequency of the element "z" is 1. Finally,

The frequency of the character "q" is 1.

By Concatenating all three strings in the array we get "xyzyyyyxq”.

Here the frequency of character ‘y’ is 5 and the sum of all frequencies of the rest of the characters is found to be 4.

Sample Example 2

Let us take the input array 
arr[]: {“mnoml", “lmll", “nln”, "mnlmn"}
Output obtained is : 2

Explanation

Here the frequency of the element or the character "m" is 5.

The frequency of the element "n" is 5 and the frequency of the element "l" is 6 and finally the frequency of the character "o" is 1.

Here we can only concatenate 2 strings "lmllnl".

The frequency of character l here is 4. The sum of frequencies of other characters’ m and n is 2. In order to rely on having concatenated strings with characters having frequency greater than sum of frequencies of other characters this is the only possible concatenation.

Approach

In Order to determine the count of strings to be concatenated with a character having frequency greater than sum of others, we take the following methodology.

The approach to solve this problem and to get the count of strings to be concatenated with a character having frequency greater than sum of others we do the iteration.

That is, we determine the net frequency of each character in all the strings by iterating for all characters, i.e., from "a" to "z." In this case, the net frequency may be computed by deducting every other rate from it, therefore if total net frequency is higher than 0, it signifies that the element's frequency exceeds the total of all the other frequencies.

Algorithm

The algorithm to determine the count of strings to be concatenated with a character having frequency greater than sum of others is given below.

  • Step 1 − Start

  • Step 2 − Define the function to determine the frequencies of all the characters and reduce the sum of other frequencies in the strings.

  • Step 3 − Iterate the array a of strings

  • Step 4 − Define an integer variable to store frequencies

  • Step 5 − Store the frequency in the array v

  • Step 6 − Define a variable to store maximum count

  • Step 7 − Iterate through all the alphabets or the elements

  • Step 8 − Return the maximum value

  • Step 9 − Stop

Example: C Program

Here is the C program implementation of the above written methodology to have count of strings to be concatenated with a character having frequency greater than sum of others.

#include <stdio.h>
#include <stdlib.h>
//input strings to be non-empty and not more //than 100 characters
#define MAX_STR_LEN 100

// Function to determine the frequencies of all the characters and reducing the sum of other frequencies in the strings
int* frequency(char** a, int len, char c){

   // We use array to store the frequency
   int* v = (int*)calloc(len, sizeof(int));
   if(v == NULL) {
      printf("Error: Memory allocation failed");
      exit(1);
   }
   
   // Iterating the array a of strings
   for (int i = 0; i < len; i++) {
      char* str = a[i];
      
      // defining an integer variable for storing //the frequencies
      int net_fre = 0;
      
      // Iterating through the string str
      for (int j = 0; str[j] != '\0'; j++) {
      
         // If str[j] is equal to the current character increment the net_fre by 1
         if (str[j] == c)
            net_fre++;
            
         // otherwise decrement net_fre by 1
         else
            net_fre--;
      }
      
      // After iterating the string store this frequency in the array v
      v[i] = net_fre;
   }
   
   //return the array v
   return v;
}

// Function to determine the count of the longest or the lengthiest string that could be obtained from the given array of strings
int longestConcatenatedString(char** a, int len){

   // An integer variable to store the maximum count Also it is set to zero
   int mxm = 0;
   
   // Iterating through all of the alphabets
   for (char c = 'a'; c <= 'z'; c++) {
   
      // Array to store the net_frequency of the character c after reducing the sum of every other frequencies in all of the strings
      int* v = frequency(a, len, c);
      
      // Array is stored in the order of descendants
      for (int i = 0; i < len - 1; i++) {
         for (int j = i + 1; j < len; j++) {
            if (v[i] < v[j]) {
               int temp = v[i];
               v[i] = v[j];
               v[j] = temp;
               char* temp_str = a[i];
               a[i] = a[j];
               a[j] = temp_str;
            }
         }
      }
      
      // Variable res is defined to store the //result
      int res = 0;
      int sum = 0;
      for (int i = 0; i < len; i++) {
         sum += v[i];
         
         // If sum is greater than 0 then increment res by 1
         if (sum > 0) {
            res++;
         }
      }
      
      // Keeping the track of the maximum one
      mxm = mxm > res ? mxm : res;
      free(v);
   }
   
   // Returning the maximum value obtained
   return mxm;
}
int main(){
   char* a[] = { "mnoml", "lmll", "nln", "mnlmn" };
   printf("Count of strings to be concatenated with a character having frequency greater than sum of others: ");
   int len = sizeof(a) / sizeof(a[0]);
   printf("%d", longestConcatenatedString(a, len));
   return 0;
}

Output

Count of strings to be concatenated with a character having frequency greater than sum of others: 2

Conclusion

Likewise, we can count strings to be concatenated with a character having frequency greater than sum of others.

The challenge of obtaining the program to count of strings to be concatenated with a character having frequency greater than sum of others is resolved in this article.

Here C++ programming code as well as the algorithm to determine the count of strings to be concatenated with a character having frequency greater than sum of others are provided.

Updated on: 10-Aug-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements