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++ implementation for the above approach #include <bits/stdc++.h> using namespace std;
// Function of find the // frequency of all characters after // reducing the sum of // other frequencies in strings vector< int > frequency(vector<string>& arr,
int ch)
{ // Vector to store frequency
vector< int > 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 strings int longestConcatenatedStr(
vector<string>& 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< int > v = frequency(arr, ch);
// Sort the vector in decreasing order
sort(v.begin(), v.end(),
greater< int >());
// 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 Code int main()
{ vector<string> arr
= { "abac" , "bacbc" , "aacab" };
cout << longestConcatenatedStr(arr);
return 0;
} |
// Java implementation for the above approach import java.util.*;
class GFG {
static String[] arr = { "abac" , "bacbc" , "aacab" };
// Function of find the
// frequency of all characters after
// reducing the sum of
// other frequencies in Strings
static Vector<Integer> frequency( int ch)
{
// Vector to store frequency
Vector<Integer> v = new Vector<>();
// Iterate over the array of Strings
for ( int i = 0 ; i < arr.length; i++) {
String s = arr[i];
// Variable to store frequencies
int net_freq = 0 ;
// Iterate over the String
for ( char x : s.toCharArray())
{
// 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.add(net_freq);
}
return v;
}
// Function to find
// the longest String that
// can be made from
// a given vector of Strings
static int longestConcatenatedStr()
{
// 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<Integer> v = frequency(ch);
// Sort the vector in decreasing order
Collections.sort(v);
Collections.reverse(v);
// Variable to store answer
int ans = 0 ;
int sum = 0 ;
for ( int 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 = Math.max(mx, ans);
}
// Return the maximum value
return mx;
}
// Driver Code
public static void main(String[] args)
{
System.out.print(longestConcatenatedStr());
}
} // This code is contributed by Rajput-Ji |
# python implementation for the above approach # Function of find the # frequency of all characters after # reducing the sum of # other frequencies in strings def 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 strings def 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 Code if __name__ = = "__main__" :
arr = [ "abac" , "bacbc" , "aacab" ]
print (longestConcatenatedStr(arr))
# This code is contributed by rakeshsahni |
using System;
using System.Collections.Generic;
// C# implementation for the above approach class GFG
{ // Function of find the
// frequency of all characters after
// reducing the sum of
// other frequencies in strings
static int [] frequency( string [] arr, int ch)
{
// List to store frequency
List< int > v = new List< int >();
// Iterate over the array of strings
for ( int i = 0; i < arr.Length; i++) {
string s = arr[i];
// Variable to store frequencies
int net_freq = 0;
// Iterate over the string
for ( int j = 0; j < s.Length; j++)
{
// If x is equal
// to current character
// increment net_freq by 1
char x = s[j];
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.Add(net_freq);
}
int [] u = v.ToArray();
return u;
}
// Function to find
// the longest string that
// can be made from
// a given vector of strings
static int longestConcatenatedStr( string [] arr)
{
// Variable to store maximum count
int mx = 0;
// Iterate over all alphabets
for ( char ch = 'a' ; ch <= 'z' ; ch++) {
// Array to store the
// net_frequency of character
// ch after reducing
// the sum of all other
// frequencies in all strings
int [] v = frequency(arr, ch);
// Sort the vector in decreasing order
Array.Sort(v);
Array.Reverse(v);
// Variable to store answer
int ans = 0;
int sum = 0;
for ( int i = 0; i < v.Length; i++) {
int x = v[i];
sum += x;
// If sum is greater than 0
// increment ans by 1
if (sum > 0) {
ans++;
}
}
// Keep track of the maximum one
mx = Math.Max(mx, ans);
}
// Return the maximum value
return mx;
}
static void Main()
{
string [] arr = { "abac" , "bacbc" , "aacab" };
Console.Write(longestConcatenatedStr(arr));
}
} // This code is contributed by garg28harsh. |
<script> // JavaScript Program to implement
// the above approach
// Function of find the
// frequency of all characters after
// reducing the sum of
// other frequencies in strings
function frequency(arr, ch) {
// Vector to store frequency
let v = [];
// Iterate over the array of strings
for (let i = 0; i < arr.length; i++) {
let s = arr[i];
// Variable to store frequencies
let net_freq = 0;
// Iterate over the string
for (let x of 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(net_freq);
}
return v;
}
// Function to find
// the longest string that
// can be made from
// a given vector of strings
function longestConcatenatedStr(
arr)
{
// Variable to store maximum count
let mx = 0;
// Iterate over all alphabets
for (let 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
let v = frequency(arr, ch);
// Sort the vector in decreasing order
v.sort( function (a, b) { return b - a })
// Variable to store answer
let ans = 0;
let sum = 0;
for (let x of v) {
sum += x;
// If sum is greater than 0
// increment ans by 1
if (sum > 0) {
ans++;
}
}
// Keep track of the maximum one
mx = Math.max(mx, ans);
}
// Return the maximum value
return mx;
}
// Driver Code
let arr
= [ "abac" , "bacbc" , "aacab" ];
document.write(longestConcatenatedStr(arr));
// This code is contributed by Potta Lokesh
</script>
|
2
Time Complexity: O(NlogN)
Auxiliary Space: O(N)