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

• Last Updated : 01 Dec, 2021

Given an array arr[] containing N strings, the task is to find the maximum number of strings that can be concatenated such that one character has a frequency greater than the sum of the frequencies of all others.

Example:

Input: arr[]: {“qpr, “pppsp, “t”}
Output: 3
Explanation: Concatenate all 3 strings to get: “aprpppspt”. The frequency of character ‘p’ is 5 and sum of all other frequencies is 4.

Input: arr[]: {“bcdba”, “abaa”, “acc”, “abcbc”}
Output:  2

Approach: To solve this problem follow the below steps:

1. Iterate for all characters, i.e. from ‘a’ to ‘z’ and in each iteration find the net frequency of that character in all strings. Here net frequency can be calculated by subtracting all other frequencies from it, which means if the net frequency is greater than 0 then that character’s frequency is more than the sum of all other frequencies. Store these frequencies in a vector v.
2. Now, sort v in decreasing order.
3. And then find for each character, the maximum number of strings that can be combined, so that the frequency of that character is greater than the sum of other frequencies.
4. Return the maximum possible answer.

Below is the implementation of the above approach:

C++

 // C++ implementation for the above approach #include using namespace std; // Function of find the// frequency of all characters after// reducing the sum of// other frequencies in stringsvector frequency(vector& arr,                      int ch){     // Vector to store frequency    vector v;     // Iterate over the array of strings    for (int i = 0; i < arr.size(); i++) {         string s = arr[i];         // Variable to store frequencies        int net_freq = 0;         // Iterate over the string        for (auto x : s) {            // If x is equal            // to current character            // increment net_freq by 1            if (x == ch)                net_freq++;             // Else decrement net_freq by 1            else                net_freq--;        }         // After the iteration of string        // store the frequency in vector        v.push_back(net_freq);    }     return v;} // Function to find// the longest string that// can be made from// a given vector of stringsint longestConcatenatedStr(    vector& arr){    // Variable to store maximum count    int mx = 0;     // Iterate over all alphabets    for (char ch = 'a'; ch <= 'z'; ch++) {         // Vector to store the        // net_frequency of character        // ch after reducing        // the sum of all other        // frequencies in all strings        vector v = frequency(arr, ch);         // Sort the vector in decreasing order        sort(v.begin(), v.end(),             greater());         // Variable to store answer        int ans = 0;         int sum = 0;        for (auto x : v) {            sum += x;             // If sum is greater than 0            // increment ans by 1            if (sum > 0) {                ans++;            }        }         // Keep track of the maximum one        mx = max(mx, ans);    }    // Return the maximum value    return mx;} // Driver Codeint main(){    vector arr        = { "abac", "bacbc", "aacab" };     cout << longestConcatenatedStr(arr);     return 0;}

Python3

 # python implementation for the above approach # Function of find the# frequency of all characters after# reducing the sum of# other frequencies in stringsdef frequency(arr, ch):     # Vector to store frequency    v = []     # Iterate over the array of strings    for i in range(0, len(arr)):         s = arr[i]         # Variable to store frequencies        net_freq = 0         # Iterate over the string        for x in s:                       # If x is equal            # to current character            # increment net_freq by 1            if (x == ch):                net_freq += 1             # Else decrement net_freq by 1            else:                net_freq -= 1         # After the iteration of string        # store the frequency in vector        v.append(net_freq)     return v  # Function to find# the longest string that# can be made from# a given vector of stringsdef longestConcatenatedStr(arr):     # Variable to store maximum count    mx = 0     # Iterate over all alphabets    for ch in range(0, 26):         # Vector to store the        # net_frequency of character        # ch after reducing        # the sum of all other        # frequencies in all strings        v = frequency(arr, chr(ch + ord('a')))         # Sort the vector in decreasing order        v.sort(reverse=True)         # Variable to store answer        ans = 0         sum = 0        for x in v:            sum += x             # If sum is greater than 0            # increment ans by 1            if (sum > 0):                ans += 1         # Keep track of the maximum one        mx = max(mx, ans)     # Return the maximum value    return mx # Driver Codeif __name__ == "__main__":     arr = ["abac", "bacbc", "aacab"]     print(longestConcatenatedStr(arr)) # This code is contributed by rakeshsahni

Javascript


Output
2

Time Complexity: O(NlogN)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up