Minimize count of repositioning of characters to make all given Strings equal


The aim here is to determine whether it would be feasible to make all of the strings identical in any amount of operations provided an array Str of strings with size n. Any element can be taken out of the string and put back in at any point in the same or another string all in one action. Return "Yes" if the strings are able to be made to equal one another, and "No" if not, along with the fewest number of operations necessary.

Problem Statement

Implement a program to minimize count of repositioning of characters to make all given Strings equal

Sample Example 1

Let us take the Input: n = 3, 
The input array, Str = {mmm, nnn, ooo}
The output obtained : Yes 6

Explanation

All the provided three strings of the array Str can be made identical to a string mno, in a minimum number of 6 operations.

{mmm, nnn, ooo} −> {mm, mnnn, ooo}
{mm, mnnn, ooo} −> {m, mnnn, mooo}
{m, mnnn, mooo} −> {mn, mnn, mooo}
{mn, mnn, mooo} −> {mn, mn, mnooo}
{mn, mn, mnooo} −> {mno, mn, mnoo}
{mno, mn, mnooo} −> {mno, mno, mno}

Sample Example 2

Let us take the Input: n = 3, 
The input array, Str = {abc, aab, bbd}
The output obtained: No

Explanation

Using the provided three strings of the array Str , no identical string can be made.

Sample Example 3

Let us take the Input: n = 3, 
The input array, Str = {xxy, zzz, xyy}
The output obtained : Yes 4

Explanation

All the provided three strings of the array Str can be made identical to a string xyz, in a minimum number of 4 operations.

Solution Approach

In Order to minimize the count of repositioning of characters to make all given Strings equal, we use the following methodology.

The approach to solve this problem and minimize count of repositioning of characters to make all given Strings equal

If the letters are distributed equally over all of the strings, the goal of making all of the strings equal can be accomplished. That is, every character's frequency in the array of strings needs to be divisible by the size "n".

Algorithm

The algorithm to minimize count of repositioning of characters to make all given Strings equal is given below

  • Step 1 − Start

  • Step 2 − Define a function to check if the strings could be made identical or not

  • Step 3 − Define an array to store the frequencies of all the characters. Here we defined "fre".

  • Step 4 − Traverse the provided array of strings.

  • Step 5 − Traverse each characters of the given string Str.

  • Step 6 − Update the frequencies obtained

  • Step 7 − now check for every character of the alphabet

  • Step 8 − If the frequency is not divisible by the size n, then print "No"

  • Step 9 − Dividing the frequency of each of the character with the size n

  • Step 10 − Define an integer variable "result" and store the result obtained as the minimum number of operations

  • Step 11 − Store the frequency of each od the characters in the original string "org"

  • Step 12 − Get the number of additional characters as well

  • Step 13 − Print Yes and the result obtained.

  • Step 14 − Stop

Example: C Program

Here is the C program implementation of the above written algorithm to minimize count of repositioning of characters to make all given Strings equal.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Define a function to check if the strings could be made identical or not
void equalOrNot(char* Str[], int n){

   // Array fre to store the frequencies of all the characters
   int fre[26] = {0};
   
   // Traverse the provided array of strings
   for (int i = 0; i < n; i++) {
   
      // Traverse each characters of the given string Str
      for (int j = 0; j < strlen(Str[i]); j++){
         // Update the frequencies obtained
         fre[Str[i][j] - 'a']++;
      }
   }
   
   // now check for every character of the alphabet
   for (int i = 0; i < 26; i++){
   
      // If the frequency is not divisible by the size n, then print No.
      if (fre[i] % n != 0){
         printf("No\n");
         return;
      }
   }
   
   // Dividing the frequency of each of the character with the size n
   for (int i = 0; i < 26; i++)
      fre[i] /= n;
      
   // Store the result obtained as the minimum number of operations
   int result = 0;
   for (int i = 0; i < n; i++) {
   
      // Store the frequency of each od the characters in the original string org
      int org[26] = {0};
      for (int j = 0; j < strlen(Str[i]); j++)
         org[Str[i][j] - 'a']++;
         
      // Get the number of additional characters as well
      for (int i = 0; i < 26; i++){
         if (fre[i] > 0 && org[i] > 0){
            result += abs(fre[i] - org[i]);
         }
      }
   }
   printf("Yes %d\n", result);
   return;
}
int main(){
   int n = 3;
   char* Str[] = { "mmm", "nnn", "ooo" };
   equalOrNot(Str, n);
   return 0;
}

Output

Yes 6

Conclusion

Likewise, we can minimize the count of repositioning of characters to make all given Strings equal.

The challenge of obtaining the program to minimize the count of repositioning of characters to make all given Strings equal is resolved in this article.

Here C programming code as well as the algorithm to minimize count of repositioning of characters to make all given Strings equal are provided.

Updated on: 10-Aug-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements