[
sort
binary search
string
]
Leetcode 1170. Compare Strings by Frequency of the Smallest Character
Problem statement
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/
Solution
For each word we evaluate frequeny of smallest symbol, then sort all these values in words
. For each element from queries
use binary search.
Complexity
It is O(n log n + m log n)
, where n = len(words)
and m = len(queries)
, space is O(m + n)
.
Code
class Solution:
def numSmallerByFrequency(self, queries, words):
f = sorted(w.count(min(w)) for w in words)
return [len(f) - bisect.bisect(f, q.count(min(q))) for q in queries]