Size of longest widespread prefix potential by rearranging strings in a given array

by admin

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int longComPre(string arr[], int N)

{

    

    

    

    int freq[N][256];

  

    

    memset(freq, 0, sizeof(freq));

  

    

    for (int i = 0; i < N; i++) {

  

        

        

        int M = arr[i].size();

  

        

        

        for (int j = 0; j < M;

             j++) {

  

            

            

            freq[i][arr[i][j]]++;

        }

    }

  

    

    

    int maxLen = 0;

  

    

    

    

    for (int j = 0; j < 256; j++) {

  

        

        

        int minRowVal = INT_MAX;

  

        

        

        

        for (int i = 0; i < N;

             i++) {

  

            

            minRowVal = min(minRowVal,

                            freq[i][j]);

        }

  

        

        maxLen += minRowVal;

    }

    return maxLen;

}

  

int primary()

{

    string arr[] = { "aabdc",

                     "abcd",

                     "aacd" };

    int N = 3;

    cout << longComPre(arr, N);

}

Related Posts

Leave a Comment