Count of strings to be concatenated with a character having frequency greater than sum of others
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:
- 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.
- Now, sort v in decreasing order.
- 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.
- Return the maximum possible answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > frequency(vector<string>& arr,
int ch)
{
vector< int > v;
for ( int i = 0; i < arr.size(); i++) {
string s = arr[i];
int net_freq = 0;
for ( auto x : s) {
if (x == ch)
net_freq++;
else
net_freq--;
}
v.push_back(net_freq);
}
return v;
}
int longestConcatenatedStr(
vector<string>& arr)
{
int mx = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
vector< int > v = frequency(arr, ch);
sort(v.begin(), v.end(),
greater< int >());
int ans = 0;
int sum = 0;
for ( auto x : v) {
sum += x;
if (sum > 0) {
ans++;
}
}
mx = max(mx, ans);
}
return mx;
}
int main()
{
vector<string> arr
= { "abac" , "bacbc" , "aacab" };
cout << longestConcatenatedStr(arr);
return 0;
}
|
Java
import java.util.*;
class GFG {
static String[] arr = { "abac" , "bacbc" , "aacab" };
static Vector<Integer> frequency( int ch)
{
Vector<Integer> v = new Vector<>();
for ( int i = 0 ; i < arr.length; i++) {
String s = arr[i];
int net_freq = 0 ;
for ( char x : s.toCharArray())
{
if (x == ch)
net_freq++;
else
net_freq--;
}
v.add(net_freq);
}
return v;
}
static int longestConcatenatedStr()
{
int mx = 0 ;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
Vector<Integer> v = frequency(ch);
Collections.sort(v);
Collections.reverse(v);
int ans = 0 ;
int sum = 0 ;
for ( int x : v) {
sum += x;
if (sum > 0 ) {
ans++;
}
}
mx = Math.max(mx, ans);
}
return mx;
}
public static void main(String[] args)
{
System.out.print(longestConcatenatedStr());
}
}
|
Python3
def frequency(arr, ch):
v = []
for i in range ( 0 , len (arr)):
s = arr[i]
net_freq = 0
for x in s:
if (x = = ch):
net_freq + = 1
else :
net_freq - = 1
v.append(net_freq)
return v
def longestConcatenatedStr(arr):
mx = 0
for ch in range ( 0 , 26 ):
v = frequency(arr, chr (ch + ord ( 'a' )))
v.sort(reverse = True )
ans = 0
sum = 0
for x in v:
sum + = x
if ( sum > 0 ):
ans + = 1
mx = max (mx, ans)
return mx
if __name__ = = "__main__" :
arr = [ "abac" , "bacbc" , "aacab" ]
print (longestConcatenatedStr(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int [] frequency( string [] arr, int ch)
{
List< int > v = new List< int >();
for ( int i = 0; i < arr.Length; i++) {
string s = arr[i];
int net_freq = 0;
for ( int j = 0; j < s.Length; j++)
{
char x = s[j];
if (x == ch)
net_freq++;
else
net_freq--;
}
v.Add(net_freq);
}
int [] u = v.ToArray();
return u;
}
static int longestConcatenatedStr( string [] arr)
{
int mx = 0;
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
int [] v = frequency(arr, ch);
Array.Sort(v);
Array.Reverse(v);
int ans = 0;
int sum = 0;
for ( int i = 0; i < v.Length; i++) {
int x = v[i];
sum += x;
if (sum > 0) {
ans++;
}
}
mx = Math.Max(mx, ans);
}
return mx;
}
static void Main()
{
string [] arr = { "abac" , "bacbc" , "aacab" };
Console.Write(longestConcatenatedStr(arr));
}
}
|
Javascript
<script>
function frequency(arr, ch) {
let v = [];
for (let i = 0; i < arr.length; i++) {
let s = arr[i];
let net_freq = 0;
for (let x of s)
{
if (x == ch)
net_freq++;
else
net_freq--;
}
v.push(net_freq);
}
return v;
}
function longestConcatenatedStr(
arr)
{
let mx = 0;
for (let ch = 'a' ; ch <= 'z' ; ch++) {
let v = frequency(arr, ch);
v.sort( function (a, b) { return b - a })
let ans = 0;
let sum = 0;
for (let x of v) {
sum += x;
if (sum > 0) {
ans++;
}
}
mx = Math.max(mx, ans);
}
return mx;
}
let arr
= [ "abac" , "bacbc" , "aacab" ];
document.write(longestConcatenatedStr(arr));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Last Updated :
22 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...