- frequency-counting, Greedy, Sorting, Strings

Maximum count of Strings to be selected such that there is a majority character

Given an array A[]of N strings of lowercase characters, the task is to find maximum number of strings, such that one character has majority i.e. the occurrence of one character in all of the strings is greater than all the other characters combined.Examples:Input: A[] = {“aba”, “abcde”, “aba”}Output: 2Explanation: On choosing {“aba”, “aba”}, the occurrence of character ‘a’ is 4 and the occurrence of rest of the characters is 2, which is less than 4. So, a maximum of 2 strings can be chosen.Input: A[] = {“bzc”, “zzzdz”, “e”}Output: 3Explanation:  All the strings can be chosen, where ‘z’ has the majority.Approach: There is a Greedy Solution to this problem. The idea is that, consider all the lowercase characters from ‘a’ to ‘z’ and for every character,  find the maximum number of strings that can be chosen, so that the occurrence of that character is greater than all the other characters combined. Maximum of them will be the answer. Now the main problem is how to find the maximum number of strings such that one particular character has majority, to solve this use greedy algorithm. The idea is that, to find the maximum number of strings for a  particular character ‘ch’, try to choose the strings in which occurrence of ‘ch’ is greatest compared to the occurrence of all the other characters, i.e. the strings in which (occurrence of ‘ch’ – occurrence of all other character) is maximum so that it will be possible to add more strings in the future.Follow the steps below to solve the problem: Define a function CalDif(string s, char ch) and perform the following tasks:Initialize the variables chcount as 0 to store the count of that character and othercount as 0 to store the count of other characters.Traverse the string s using the variable x and perform the following tasks:If character at current position is ch, then increase the value of chcount by 1 else increase the value of othercount by 1.Return the difference between chcount and othercount.Initialize the variable ans and assign 0, it will store the final answer.Iterate over the lower-case character range [a, z] using the variable i and perform the following steps: Initialize a vector arr[] of length N, where arr[i] will store (occurrence of ch – occurrence of all other character) for i-th string.Run a loop from i=0 to N-1For the ith string compute (occurrence of ch – occurrence of all other character) using the function CalDiff(A[i], ch) and assign it to arr[i].Sort arr[] in decreasing order.Initialize two variables temp and count and assign 0 to them.Traverse the array arr[] and do temp += arr[i] and count++, where i starts from 0, until temp