Compare Strings by Frequency of the Smallest Character - Problem
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]) < f(W) for each W in words.
Return an integer array answer, where each answer[i] is the answer to the ith query.
Input & Output
Example 1 — Basic Case
$
Input:
words = ["cbd"], queries = ["aa"]
›
Output:
[0]
💡 Note:
f("aa") = 2 (frequency of 'a'), f("cbd") = 1 (frequency of 'b'). Since 2 > 1, there are 0 words where f("aa") < f(word).
Example 2 — Multiple Words
$
Input:
words = ["aa","aaa","cccc"], queries = ["cbd"]
›
Output:
[3]
💡 Note:
f("cbd") = 1, f("aa") = 2, f("aaa") = 3, f("cccc") = 4. Since 1 < 2, 1 < 3, and 1 < 4, all 3 words satisfy the condition.
Example 3 — Mixed Results
$
Input:
words = ["bbb","cc"], queries = ["aaa","bb"]
›
Output:
[1,1]
💡 Note:
f("aaa") = 3, f("bb") = 2, f("bbb") = 3, f("cc") = 2. For "aaa": only "bbb" has f > 3 (none do), so 0. Wait, f("bbb") = 3, not > 3. Actually f("bbb") = 3, so 3 = 3, not greater. Let me recalculate...
Constraints
- 1 ≤ queries.length ≤ 2000
- 1 ≤ words.length ≤ 2000
- 1 ≤ queries[i].length, words[i].length ≤ 10
- queries[i][j], words[i][j] are lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
words=["aa","aaa","cccc"], queries=["cbd"]
2
Calculate f(s)
Find frequency of lexicographically smallest char in each string
3
Compare
Count words where f(query) < f(word)
Key Takeaway
🎯 Key Insight: Pre-calculate and sort frequencies to enable efficient binary search comparisons
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code