Given two array of strings, **arr1[]** and **arr2[]**, the task is to count the number of string in **arr2[]** whose frequency of smallest characters is less than frequency of smallest character for each string in **arr1[]**.

**Examples:**

Input:arr1[] = {“cbd”}, arr2[] = {“zaaaz”}Output:1Explanation:

Frequency of smallest characters in “cbd” is 1 which is less than the frequency of smallest characters in “zaaaz” which is 2.

Therefore the total count is 1 for string “cbd”.

Input:arr1[] = {“yyy”,”zz”}, arr2[] = {“x”,”xx”,”xxx”,”xxxx”}Output:1 2Explanation:

1. frequency of smallest characters in “yyy” is 3 which is less than the frequency of smallest characters in “xxxx” which is 4.

Therefore the total count is 1 for string “yyy”.

2. frequency of smallest characters in “zz” is 2 which is less than the frequency of smallest characters in “xxx” and “xxxx” which is 3 and 4 respectively.

Therefore the total count is 2 for string “zz”.

**Approach:** This problem can be solved using Greedy Approach. Below are the steps:

- For each string in the array
**arr2[]**count the frequency of smallest characters and store it in the array (say**freq[]**). - Sort the frequency array
**freq[]**. - Now for each string in the array
**arr1[]**count the frequency of smallest characters in the string (say**X**). - For each
**X**, find the number of elements in greater than X in**freq[]**using Binary Search by using the approach discussed in this article.

Below is the implementation of the above approach:

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to count the frequency of ` `// minimum character ` `int` `countMinFreq(string s) `
`{ ` ` ` ` ` `// Sort the string s `
` ` `sort(s.begin(), s.end()); `
` ` ` ` `// Return the count with smallest `
` ` `// character `
` ` `return` `count(s.begin(), s.end(), s[0]); `
`} ` ` ` `// Function to count number of frequency ` `// of smallest character of string arr1[] ` `// is less than the string in arr2[] ` `void` `countLessThan(vector<string>& arr1, `
` ` `vector<string>& arr2) `
`{ ` ` ` `// To store the frequency of smallest `
` ` `// character in each string of arr2 `
` ` `vector<` `int` `> freq; `
` ` ` ` `// Traverse the arr2[] `
` ` `for` `(string s : arr2) { `
` ` ` ` `// Count the frequency of smallest `
` ` `// character in string s `
` ` `int` `f = countMinFreq(s); `
` ` ` ` `// Append the frequency to freq[] `
` ` `freq.push_back(f); `
` ` `} `
` ` ` ` `// Sort the frequency array `
` ` `sort(freq.begin(), freq.end()); `
` ` ` ` `// Traverse the array arr1[] `
` ` `for` `(string s : arr1) { `
` ` ` ` `// Count the frequency of smallest `
` ` `// character in string s `
` ` `int` `f = countMinFreq(s); `
` ` ` ` `// find the element greater than f `
` ` `auto` `it = upper_bound(freq.begin(), `
` ` `freq.end(), f); `
` ` ` ` `// Find the count such that `
` ` `// arr1[i] < arr2[j] `
` ` `int` `cnt = freq.size() `
` ` `- (it - freq.begin()); `
` ` ` ` `// Print the count `
` ` `cout << cnt << ` `' '` `; `
` ` `} `
`} ` ` ` `// Driver Code ` `int` `main() `
`{ ` ` ` ` ` `vector<string> arr1, arr2; `
` ` `arr1 = { ` `"yyy"` `, ` `"zz"` `}; `
` ` `arr2 = { ` `"x"` `, ` `"xx"` `, ` `"xxx"` `, ` `"xxxx"` `}; `
` ` ` ` `// Function Call `
` ` `countLessThan(arr1, arr2); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python3 program for the above approach ` `from` `bisect ` `import` `bisect_right as upper_bound `
` ` `# Function to count the frequency ` `# of minimum character ` `def` `countMinFreq(s): `
` ` ` ` `# Sort the string s `
` ` `s ` `=` `sorted` `(s) `
` ` ` ` `# Return the count with smallest `
` ` `# character `
` ` `x ` `=` `0`
` ` `for` `i ` `in` `s: `
` ` `if` `i ` `=` `=` `s[` `0` `]: `
` ` `x ` `+` `=` `1`
` ` `return` `x `
` ` `# Function to count number of frequency ` `# of smallest character of string arr1[] ` `# is less than the string in arr2[] ` `def` `countLessThan(arr1, arr2): `
` ` ` ` `# To store the frequency of smallest `
` ` `# character in each string of arr2 `
` ` `freq ` `=` `[] `
` ` ` ` `# Traverse the arr2[] `
` ` `for` `s ` `in` `arr2: `
` ` ` ` `# Count the frequency of smallest `
` ` `# character in string s `
` ` `f ` `=` `countMinFreq(s) `
` ` ` ` `# Append the frequency to freq[] `
` ` `freq.append(f) `
` ` ` ` `# Sort the frequency array `
` ` `feq ` `=` `sorted` `(freq) `
` ` ` ` `# Traverse the array arr1[] `
` ` `for` `s ` `in` `arr1: `
` ` ` ` `# Count the frequency of smallest `
` ` `# character in string s `
` ` `f ` `=` `countMinFreq(s); `
` ` ` ` `# find the element greater than f `
` ` `it ` `=` `upper_bound(freq,f) `
` ` ` ` `# Find the count such that `
` ` `# arr1[i] < arr2[j] `
` ` `cnt ` `=` `len` `(freq)` `-` `it `
` ` ` ` `# Print the count `
` ` `print` `(cnt, end ` `=` `" "` `) `
` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: `
` ` ` ` `arr1 ` `=` `[` `"yyy"` `, ` `"zz"` `] `
` ` `arr2 ` `=` `[ ` `"x"` `, ` `"xx"` `, ` `"xxx"` `, ` `"xxxx"` `] `
` ` ` ` `# Function Call `
` ` `countLessThan(arr1, arr2); `
` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

1 2

* Time Complexity: O(N + M*log M)*, where N and M is the length of given arrays respectively.

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.

## Recommended Posts:

- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Longest sub-string having frequency of each character less than equal to k
- Highest and Smallest power of K less than and greater than equal to N respectively
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if frequency of each digit is less than the digit
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find the character in first string that is present at minimum index in second string
- Check if the frequency of any character is more than half the length of the string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Count of strings in the first array which are smaller than every string in the second array
- Count number of rotated strings which have more number of vowels in the first half than second half
- Smallest subarray having an element with frequency greater than that of other elements
- Smallest even digits number not less than N
- Smallest odd digits number not less than N
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Count of alphabets having ASCII value less than and greater than k
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count the number of words having sum of ASCII values less than and greater than k
- Largest number less than N with digit sum greater than the digit sum of N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.