<jupyter_start><jupyter_text>说明：    让我们在一个非空字符串s上定义一个函数f（s），该函数计算s中最小字符的频率。    例如，如果s =“ dcce”，则f（s）= 2，    因为最小字符为“ c”，其频率为2。    现在，给定字符串数组查询和单词，返回整数数组答案，    其中每个答案[i]是 f(queries[i]) < f(W) 的单词数，其中W是单词中的单词。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") < f("zaaaz").Example 2:    Input: queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]    Output: [1,2]    Explanation: On the first query only f("bbb") < f("aaaa").                  On the second query both f("aaa") and f("aaaa") are both > f("cc").Constraints:     1、1 <= queries.length <= 2000    2、1 <= words.length <= 2000    3、1 <= queries[i].length, words[i].length <= 10    4、queries[i][j], words[i][j] are English lowercase letters.<jupyter_code>from collections import Counter 

class Solution:
    def numSmallerByFrequency(self, queries, words):
        ans = []
        q_freq = [self.helper(word) for word in queries]
        w_freq = [self.helper(word) for word in words]
        print(q_freq, w_freq)
        for i in range(len(q_freq)):
            cnt = 0
            for j in range(len(w_freq)):
                if q_freq[i] < w_freq[j]:
                    cnt += 1
            if cnt != 0:
                ans.append(cnt)
        return ans
    
    def helper(self, word):
        # 返回word中最小字符的频率
        w_freq = Counter(word)
        w_freq = sorted(w_freq.items(), key=lambda x: x[0])
        return w_freq[0][1]
solution = Solution()
solution.numSmallerByFrequency(queries = ["bba","abaaaaaa","aaaaaa","bbabbabaab","aba","aa","baab","bbbbbb","aab","bbabbaabb"],
                               words = ["aaabbb","aab","babbab","babbbb","b","bbbbbbbbab","a","bbbbbbbbbb","baaabbaab","aa"])
a = [6,1,1,2,3,3,3,1,3,2]
print(len(a))
q = [1, 7, 6, 4, 2, 2, 2, 6, 2, 3] 
w = [3, 2, 2, 1, 1, 1, 1, 10, 5, 2]
# f(queries[i]) < f(W)
class Solution:
    def numSmallerByFrequency(self, queries, words):
        def str2num(ls):
            q = []
            for i in range(len(ls)):
                min_char = 'z'
                count = 0
                for j in range(len(ls[i])):
                    if ord(ls[i][j]) < ord(min_char):
                        count = 1
                        min_char = ls[i][j]
                    elif ord(ls[i][j]) == ord(min_char):
                        count += 1
                q.append(count)
            return q
        
        def bin_search(a):
            l, r = 0, len(w)
            while l < r:
                mid = (l+r)//2
                if w[mid] <= a:
                    l = mid + 1
                else:
                    r = mid
            return l
            
        q = str2num(queries)
        w = str2num(words)
        w.sort()
        res = []
        for i in range(len(q)):
            res.append(len(w) - bin_search(q[i]))
        return res
solution = Solution()
solution.numSmallerByFrequency(queries = ["bba","abaaaaaa","aaaaaa","bbabbabaab","aba","aa","baab","bbbbbb","aab","bbabbaabb"],
                               words = ["aaabbb","aab","babbab","babbbb","b","bbbbbbbbab","a","bbbbbbbbbb","baaabbaab","aa"])<jupyter_output>[1, 7, 6, 4, 2, 2, 2, 6, 2, 3] [3, 2, 2, 1, 1, 1, 1, 10, 5, 2]
StarcoderdataJupyterStructuredCleanDedup