Compare Strings by Frequency of the Smallest Character - Problem

Imagine you're a linguist analyzing the characteristics of different languages! You have a special function f(s) that finds the lexicographically smallest character in a string and returns how many times it appears.

For example, if s = "dcce", then f(s) = 2 because the smallest character is 'c' (comes first alphabetically among d, c, c, e), and it appears 2 times.

You're given two arrays:

  • words - An array of strings to analyze
  • queries - An array of query strings

For each query queries[i], count how many words in words have a larger f-value than the query. In other words, find how many words W satisfy f(queries[i]) < f(W).

Goal: Return an array where answer[i] is the count for the i-th query.

Input & Output

example_1.py โ€” Basic Example
$ Input: queries = ["cbd"], words = ["zaaaz"]
โ€บ Output: [1]
๐Ÿ’ก Note: f("cbd") = 1 (char 'b' appears once), f("zaaaz") = 3 (char 'a' appears 3 times). Since 1 < 3, we count 1 word.
example_2.py โ€” Multiple Queries
$ Input: queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
โ€บ Output: [1,2]
๐Ÿ’ก Note: f("bbb") = 3, f("cc") = 2. For words: f("a")=1, f("aa")=2, f("aaa")=3, f("aaaa")=4. Query "bbb": only "aaaa" has f-value > 3. Query "cc": "aaa" and "aaaa" have f-values > 2.
example_3.py โ€” Edge Case
$ Input: queries = ["bba"], words = ["aaa","aa","aaba"]
โ€บ Output: [2]
๐Ÿ’ก Note: f("bba") = 2 (char 'a' appears twice). f("aaa")=3, f("aa")=2, f("aaba")=3. Words "aaa" and "aaba" have f-values > 2.

Constraints

  • 1 โ‰ค queries.length โ‰ค 2000
  • 1 โ‰ค words.length โ‰ค 2000
  • 1 โ‰ค queries[i].length, words[i].length โ‰ค 10
  • queries[i][j], words[i][j] consist of lowercase English letters only

Visualization

Tap to expand
String F-Value Analysis ProcessStep 1: Calculate F-Values"dcce" โ†’ min='c', count=2"aaba" โ†’ min='a', count=3"bb" โ†’ min='b', count=2"zz" โ†’ min='z', count=2Step 2: Sort F-Values[2, 2, 2, 3]Sorted array readyfor binary searchStep 3: Query ProcessingQuery: "ab" โ†’ f=1Binary search: findposition where f>1Result: 4 wordsBinary Search Visualization2223Query f=1, looking for f>1All 4 elements > 1!๐ŸŽฏ Key Insight: Sorting enables O(log n) query processing instead of O(n)
Understanding the Visualization
1
Calculate F-Values
For each string, find the lexicographically smallest character and count its occurrences
2
Sort Word F-Values
Create a sorted array of all word f-values for efficient searching
3
Binary Search Queries
For each query, use binary search to find how many words have higher f-values
Key Takeaway
๐ŸŽฏ Key Insight: Pre-sorting the f-values transforms each query from O(n) linear search to O(log n) binary search, dramatically improving performance for multiple queries.
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 18
28.4K Views
Medium Frequency
~15 min Avg. Time
947 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen