Let the function f(s) be the frequency of the lexicographically smallest character in a non-empty string s. For example, if s = "dcce" then f(s) = 2 because the lexicographically smallest character is 'c', which has a frequency of 2.

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

Return an integer array answer, where each answer[i] is the answer to the ith query.

&nbsp;
Example 1:

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


Example 2:

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


&nbsp;
Constraints:


	1 &lt;= queries.length &lt;= 2000
	1 &lt;= words.length &lt;= 2000
	1 &lt;= queries[i].length, words[i].length &lt;= 10
	queries[i][j], words[i][j] consist of lowercase English letters.


class Solution:
    def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
        def _f(s):
            d = Counter(s)
            d =dict(sorted(d.items(), key=lambda item: item[0]))
            for x in d:
                return d[x]
        
        freq = []
        for w in words:
            n1 = _f(w)
            freq.append(n1)
        
        freq.sort(reverse=True)

        res = []
        for q in queries:
            n = _f(q)
            c=0
            for n1 in freq:
                if n < n1:
                    c+=1
                else:
                    break
            res.append(c)
        
        return res
class Solution {
    public int[] numSmallerByFrequency(String[] queries, String[] words) {
        int[] ans = new int[queries.length];
        int[] freq = new int[words.length];
        for (int i = 0; i < words.length; i++) {
            freq[i] = freqOfSmallest(words[i]);
        }
        Arrays.sort(freq);
        int k = 0;
        for (String query : queries) {
            int target = freqOfSmallest(query);
            ans[k++] = binarySearch(freq, target);
        }
        return ans;
    }
    public int freqOfSmallest(String s) {
        int[] freq = new int[26];
        char min = 'z';
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            freq[c - 'a'] += 1;
            if (c < min) {
                min = c;
            }
        }
        return freq[min - 'a'];
    }
    public int binarySearch(int[] arr, int target) {
        int idx = arr.length;
        int lo = 0;
        int hi = idx - 1;
        int mid;
        while (lo <= hi) {
            mid = (lo + hi) / 2;
            if (arr[mid] <= target) {
                lo = mid + 1;
            } else {
                idx = mid;
                hi = mid - 1;
            }
        }
        return arr.length - idx;
    }
}
class Solution {
private:
    int countFreq(const string &s) {
        char c = *min_element(begin(s), end(s));
        return count(begin(s), end(s), c);
    }
public:
    vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
        vector<int> freqs(11, 0);
        for (const auto &word : words) {
            freqs[countFreq(word)]++;
        }
        vector<int> prefSum(12, 0);
        for (int i = 0; i < freqs.size(); i++) {
            prefSum[i+1] = prefSum[i] + freqs[i];
        }
        vector<int> ans;
        for (const auto &q : queries) {
            int cnt = countFreq(q);
            ans.push_back(prefSum.back() - prefSum[cnt + 1]);
        }
        return ans;
    }
};
/**
 * @param {string[]} queries
 * @param {string[]} words
 * @return {number[]}
 */
var numSmallerByFrequency = function(queries, words) {
    let res=[]
    queries=queries.map(q=>countFrequency(q))//[3,2]
    words=words.map(w=>countFrequency(w))//[1,2,3,4]
  words = words.sort((a, b) => a - b)
    for(let i=0;i<queries.length;i++){
      let start=0,end=words.length-1
      while(start<=end){
          let mid=start+Math.floor((end-start)/2)
          if(queries[i]<words[mid]){
              end=mid-1
          }else{
              start=mid+1
          }
      }
      res.push(words.length-1-end)
  }
    return res
};

function countFrequency(str){
    let freq={}
    str.split('').forEach(key=>{
        freq[key]=(freq[key]||0)+1
    })
    return freq[Object.keys(freq).sort()[0]]
}
Compare Strings by Frequency of the Smallest Character