# Frequency of smallest character in first sentence less than that of second sentence

• Difficulty Level : Easy
• Last Updated : 14 Sep, 2022

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

Examples:

Input: arr1[] = {“cbd”}, arr2[] = {“zaaaz”}
Output:
Explanation:
The frequency of the smallest characters in “cbd” is 1 which is less than the frequency of the smallest characters in “zaaaz” which is 2.
Therefore, the total count is 1 for the string “cbd”.

Input: arr1[] = {“yyy”,”zz”}, arr2[] = {“x”,”xx”,”xxx”,”xxxx”}
Output: 1 2
Explanation:
1. The frequency of the smallest characters in “yyy” is 3 which is less than the frequency of the smallest characters in “xxxx” which is 4.
Therefore, the total count is 1 for the string “yyy”.
2. The frequency of the smallest characters in “zz” is 2 which is less than the frequency of the smallest characters in “xxx” and “xxxx” which is 3 and 4 respectively.
Therefore, the total count is 2 for the string “zz”.

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

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

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``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& arr1,``                   ``vector& 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 arr1, arr2;``    ``arr1 = { ``"yyy"``, ``"zz"` `};``    ``arr2 = { ``"x"``, ``"xx"``, ``"xxx"``, ``"xxxx"` `};` `    ``// Function Call``    ``countLessThan(arr1, arr2);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;``class` `GFG``{` `  ``// Function to count the frequency of``  ``// minimum character``  ``static` `int` `countMinFreq(String s)``  ``{` `    ``// Sort the string s``    ``char``[] tempArray = s.toCharArray();``    ``Arrays.sort(tempArray);``    ``s = ``new` `String(tempArray);` `    ``// Return the count with smallest``    ``// character``    ``int` `x = ``0``;``    ``for` `(``int` `i = ``0``; i < s.length(); i++)``      ``if` `(s.charAt(i) == s.charAt(``0``))``        ``x++;``    ``return` `x;``  ``}` `  ``// Function to count number of frequency``  ``// of smallest character of string arr1[]``  ``// is less than the string in arr2[]``  ``static` `void` `countLessThan(List arr1,``                            ``List arr2)``  ``{` `    ``// To store the frequency of smallest``    ``// character in each string of arr2``    ``List freq = ``new` `ArrayList();` `    ``// 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.add(f);``    ``}` `    ``// Sort the frequency array``    ``Collections.sort(freq);` `    ``// 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``      ``int` `it = upper_bound(freq, f);` `      ``// Find the count such that``      ``// arr1[i] < arr2[j]``      ``int` `cnt = freq.size() - it;` `      ``// Print the count``      ``System.out.print(cnt + ``" "``);``    ``}``  ``}` `  ``static` `int` `upper_bound(List freq, ``int` `f)``  ``{``    ``int` `low = ``0``, high = freq.size() - ``1``;` `    ``while` `(low < high) {``      ``int` `mid = (low + high) / ``2``;``      ``if` `(freq.get(mid) > f)``        ``high = mid;``      ``else``        ``low = mid + ``1``;``    ``}` `    ``return` `(freq.get(low) < f) ? low++ : low;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main(String[] args)``  ``{``    ``List arr1, arr2;``    ``arr1 = Arrays.asList(``new` `String[] { ``"yyy"``, ``"zz"` `});``    ``arr2 = Arrays.asList(``      ``new` `String[] { ``"x"``, ``"xx"``, ``"xxx"``, ``"xxxx"` `});` `    ``// Function Call``    ``countLessThan(arr1, arr2);``  ``}``}` `// This code is contributed by jithin.`

## Python3

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

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG {` `  ``// Function to count the frequency of``  ``// minimum character``  ``static` `int` `countMinFreq(String s)``  ``{` `    ``// Sort the string s``    ``char``[] tempArray = s.ToCharArray();``    ``Array.Sort(tempArray);``    ``s = ``new` `string``(tempArray);` `    ``// Return the count with smallest``    ``// character``    ``int` `x = 0;``    ``for` `(``int` `i = 0; i < s.Length; i++)``      ``if` `(s[i] == s[0])``        ``x++;``    ``return` `x;``  ``}` `  ``// Function to count number of frequency``  ``// of smallest character of string arr1[]``  ``// is less than the string in arr2[]``  ``static` `void` `countLessThan(List<``string``> arr1,``                            ``List<``string``> arr2)``  ``{` `    ``// To store the frequency of smallest``    ``// character in each string of arr2``    ``List<``int``> freq = ``new` `List<``int``>();` `    ``// Traverse the arr2[]``    ``foreach``(``string` `s ``in` `arr2)``    ``{` `      ``// Count the frequency of smallest``      ``// character in string s``      ``int` `f = countMinFreq(s);` `      ``// Append the frequency to freq[]``      ``freq.Add(f);``    ``}` `    ``// Sort the frequency array``    ``freq.Sort();` `    ``// Traverse the array arr1[]``    ``foreach``(``string` `s ``in` `arr1)``    ``{` `      ``// Count the frequency of smallest``      ``// character in string s``      ``int` `f = countMinFreq(s);` `      ``// find the element greater than f``      ``int` `it = upper_bound(freq, f);` `      ``// Find the count such that``      ``// arr1[i] < arr2[j]``      ``int` `cnt = freq.Count - it;` `      ``// Print the count``      ``Console.Write(cnt + ``" "``);``    ``}``  ``}` `  ``static` `int` `upper_bound(List<``int``> freq, ``int` `f)``  ``{``    ``int` `low = 0, high = freq.Count - 1;` `    ``while` `(low < high) {``      ``int` `mid = (low + high) / 2;``      ``if` `(freq[mid] > f)``        ``high = mid;``      ``else``        ``low = mid + 1;``    ``}` `    ``return` `(freq[low] < f) ? low++ : low;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(``string``[] args)``  ``{``    ``List<``string``> arr1 = ``new` `List<``string``>();``    ``List<``string``> arr2 = ``new` `List<``string``>();``    ``arr1.Add(``"yyy"``);``    ``arr1.Add(``"zz"``);``    ``arr2.Add(``"x"``);``    ``arr2.Add(``"xx"``);``    ``arr2.Add(``"xxx"``);``    ``arr2.Add(``"xxxx"``);` `    ``// Function Call``    ``countLessThan(arr1, arr2);``  ``}``}` `// This code is contributed by phasing17.`

## Javascript

 ``

Output:

`1 2`

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

Auxiliary Space: O(M)

My Personal Notes arrow_drop_up