# 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:**

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

Input:arr[]: {“qpr, “pppsp, “t”}Output:3Explanation: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++

`// 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;` `}` |

## Python3

`# 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` |

## Javascript

`<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>` |

**Output**

2

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