Maximum count of Strings to be selected such that there is a majority character
Last Updated :
31 Aug, 2021
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: 2
Explanation: 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: 3
Explanation: 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-1
- For 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 <= 0.
- Set the value of ans max of ans and count.
- After performing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CalDiff(string s, char ch)
{
int chcount = 0;
int othercount = 0;
for ( auto x : s) {
if (x == ch)
chcount++;
else
othercount++;
}
return (chcount - othercount);
}
int MaximumNumberOfString(string A[], int N)
{
int ans = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
vector< int > arr(N);
for ( int i = 0; i < N; i++) {
arr[i] = CalDiff(A[i], ch);
}
sort(arr.begin(), arr.end(), greater< int >());
int temp = 0, count = 0;
temp += arr[0];
int j = 1;
while (temp > 0) {
count++;
if (j != N)
temp += arr[j++];
else
break ;
}
ans = max(ans, count);
}
return ans;
}
int main()
{
string A[] = { "aba" , "abcde" , "aba" };
int N = sizeof (A) / sizeof (A[0]);
cout << MaximumNumberOfString(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int CalDiff(String s, char ch)
{
int chcount = 0 ;
int othercount = 0 ;
for ( int x : s.toCharArray())
{
if (x == ch)
chcount++;
else
othercount++;
}
return (chcount - othercount);
}
static int MaximumNumberOfString(String A[], int N)
{
int ans = 0 ;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
int []arr = new int [N];
for ( int i = 0 ; i < N; i++) {
arr[i] = CalDiff(A[i], ch);
}
Arrays.sort(arr);
arr = reverse(arr);
int temp = 0 , count = 0 ;
temp += arr[ 0 ];
int j = 1 ;
while (temp > 0 )
{
count++;
if (j != N)
temp += arr[j++];
else
break ;
}
ans = Math.max(ans, count);
}
return ans;
}
static int [] reverse( int a[]) {
int i, n = a.length, t;
for (i = 0 ; i < n / 2 ; i++) {
t = a[i];
a[i] = a[n - i - 1 ];
a[n - i - 1 ] = t;
}
return a;
}
public static void main(String[] args)
{
String A[] = { "aba" , "abcde" , "aba" };
int N = A.length;
System.out.print(MaximumNumberOfString(A, N));
}
}
|
Python3
def CalDiff(s, ch):
chcount = 0
othercount = 0
for x in s:
if (x = = ch):
chcount + = 1
else :
othercount + = 1
return (chcount - othercount)
def MaximumNumberOfString(A, N):
ans = 0
for ch in range ( 97 , 123 , 1 ):
arr = [ 0 for i in range (N)]
for i in range (N):
arr[i] = CalDiff(A[i], chr (ch))
arr.sort(reverse = True )
temp = 0
count = 0
temp + = arr[ 0 ]
j = 1
while (temp > 0 ):
count + = 1
if (j ! = N):
temp + = arr[j]
j + = 1
else :
break
ans = max (ans, count)
return ans
if __name__ = = '__main__' :
A = [ "aba" , "abcde" , "aba" ]
N = len (A)
print (MaximumNumberOfString(A, N))
|
C#
using System;
class GFG{
static int CalDiff( string s, char ch)
{
int chcount = 0;
int othercount = 0;
foreach ( int x in s.ToCharArray())
{
if (x == ch)
chcount++;
else
othercount++;
}
return (chcount - othercount);
}
static int MaximumNumberOfString( string [] A, int N)
{
int ans = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++)
{
int []arr = new int [N];
for ( int i = 0; i < N; i++)
{
arr[i] = CalDiff(A[i], ch);
}
Array.Sort(arr);
arr = reverse(arr);
int temp = 0, count = 0;
temp += arr[0];
int j = 1;
while (temp > 0)
{
count++;
if (j != N)
temp += arr[j++];
else
break ;
}
ans = Math.Max(ans, count);
}
return ans;
}
static int [] reverse( int [] a)
{
int i, n = a.Length, t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
public static void Main(String []args)
{
string [] A = { "aba" , "abcde" , "aba" };
int N = A.Length;
Console.WriteLine(MaximumNumberOfString(A, N));
}
}
|
Javascript
<script>
function CalDiff(s, ch) {
let chcount = 0;
let othercount = 0;
for (let x of s) {
if (x == ch) chcount++;
else othercount++;
}
return chcount - othercount;
}
function MaximumNumberOfString(A, N) {
let ans = 0;
for (let ch = "a" .charCodeAt(0); ch <= "z" .charCodeAt(0); ch++) {
let arr = new Array(N);
for (let i = 0; i < N; i++) {
arr[i] = CalDiff(A[i], String.fromCharCode(ch));
}
arr.sort((a, b) => b - a);
let temp = 0,
count = 0;
temp += arr[0];
let j = 1;
while (temp > 0) {
count++;
if (j != N) temp += arr[j++];
else break ;
}
ans = Math.max(ans, count);
}
return ans;
}
let A = [ "aba" , "abcde" , "aba" ];
let N = A.length;
document.write(MaximumNumberOfString(A, N));
</script>
|
Time Complexity: O(N * |s|), where |s| is maximum string length.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...