<p>Let the function <code>f(s)</code> be the <strong>frequency of the lexicographically smallest character</strong> in a non-empty string <code>s</code>. For example, if <code>s = &quot;dcce&quot;</code> then <code>f(s) = 2</code> because the lexicographically smallest character is <code>&#39;c&#39;</code>, which has a frequency of 2.</p>

<p>You are given an array of strings <code>words</code> and another array of query strings <code>queries</code>. For each query <code>queries[i]</code>, count the <strong>number of words</strong> in <code>words</code> such that <code>f(queries[i])</code> &lt; <code>f(W)</code> for each <code>W</code> in <code>words</code>.</p>

<p>Return <em>an integer array </em><code>answer</code><em>, where each </em><code>answer[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> queries = [&quot;cbd&quot;], words = [&quot;zaaaz&quot;]
<strong>Output:</strong> [1]
<strong>Explanation:</strong> On the first query we have f(&quot;cbd&quot;) = 1, f(&quot;zaaaz&quot;) = 3 so f(&quot;cbd&quot;) &lt; f(&quot;zaaaz&quot;).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> queries = [&quot;bbb&quot;,&quot;cc&quot;], words = [&quot;a&quot;,&quot;aa&quot;,&quot;aaa&quot;,&quot;aaaa&quot;]
<strong>Output:</strong> [1,2]
<strong>Explanation:</strong> On the first query only f(&quot;bbb&quot;) &lt; f(&quot;aaaa&quot;). On the second query both f(&quot;aaa&quot;) and f(&quot;aaaa&quot;) are both &gt; f(&quot;cc&quot;).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= queries.length &lt;= 2000</code></li>
	<li><code>1 &lt;= words.length &lt;= 2000</code></li>
	<li><code>1 &lt;= queries[i].length, words[i].length &lt;= 10</code></li>
	<li><code>queries[i][j]</code>, <code>words[i][j]</code> consist of lowercase English letters.</li>
</ul>

class Solution {
private:
    int FindFrequencyOfSmallestChar(string str){
        int freq[26] = { 0 };
        int mn(INT_MAX); //minimum
        //get smallest character and the frequency of all characters in str
        for(int i = 0; i < str.size(); i++){
            mn = min(mn, str[i] - 'a');
            freq[str[i] - 'a']++;
        }
        //return the frequency of smallest char
        return freq[mn];
    }

public:
    vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
        /*Test cases are:
        
        queries = ["cbd"]
        words = ["zaaaz"]
        Expected: [1]

        queries = ["bbb", "cc"]
        words = ["a", "aa", "aaa", "aaaa"]
        Expected: [1,2]
        */
        //get queries and words size
        int x(queries.size()), y(words.size());

        vector<int> wordsFunction, queriesFunction, ans;
        //add the smallest char to wordsFunction to the largest 
        for(int i = 0; i< y; ++i){
            wordsFunction.push_back(FindFrequencyOfSmallestChar(words[i]));
        }
        //add the smallest char to the queriesFunction to the largest
        for(int i = 0; i < x; ++i){
            queriesFunction.push_back(FindFrequencyOfSmallestChar(queries[i]));
        }

        //sort the vector
        sort(wordsFunction.begin(), wordsFunction.end());
        //add to the answer vector with correct evaluations
        for(int i = 0; i < queriesFunction.size(); ++i){
            auto it = upper_bound(wordsFunction.begin(), wordsFunction.end(), queriesFunction[i]) - wordsFunction.begin();
            ans.push_back(wordsFunction.size() - it);
        }
        //return the sorted answer vector
        return ans;
    }
};
16600