LeetCode Problem

How to Solve Compare Strings by Frequency of the Smallest Character

Start by computing the frequency of the smallest character for each word and store them in a sorted array for efficient comparison. Then, for each query, determine its smallest character frequency and count how many words exceed it using binary search or scanning. This method ensures the solution leverages array scanning plus hash lookup to handle multiple queries quickly without redundant string traversals.

GhostInterview Help

Need help with Compare Strings by Frequency of the Smallest Character without spending extra time grinding it?

GhostInterview can read Compare Strings by Frequency of the Smallest Character from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1170Array scanning plus hash lookupReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Start by computing the frequency of the smallest character for each word and store them in a sorted array for efficient comparison. Then, for each query, determine its smallest character frequency and count how many words exceed it using binary search or scanning. This method ensures the solution leverages array scanning plus hash lookup to handle multiple queries quickly without redundant string traversals.

Problem Statement

Given two arrays of non-empty lowercase strings, words and queries, define f(s) as the frequency of the lexicographically smallest character in string s. For example, f("dcce") = 2 because 'c' is smallest and appears twice. Your task is to process each query in queries and determine how many words have a strictly higher f value.

Return an integer array answer where answer[i] represents the count of words satisfying f(queries[i]) < f(W) for each W in words. Constraints include up to 2000 queries and words, each string up to length 10, consisting of lowercase letters only.

Examples

Example 1

Input: queries = ["cbd"], words = ["zaaaz"]

Output: [1]

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]

On the first query only f("bbb") f("cc").

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.

Solution Approach

Precompute Word Frequencies

Scan each string in words to compute f(W) and store the results in an integer array. Sorting this array enables faster comparison later, turning multiple string scans into efficient numeric lookups.

Evaluate Queries

For each query string, compute f(query) using the same scanning method. Use the precomputed sorted word frequencies to count how many are strictly larger, either via binary search or a simple iteration, directly leveraging the array scanning plus hash lookup pattern.

Optimize for Multiple Queries

Since words are static, sorting their frequencies once and reusing the array avoids redundant computation. This trade-off balances time and space, ensuring that handling up to 2000 queries remains efficient while maintaining clarity in the scanning and counting logic.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(NlogN + MlogN) where N is the number of words and M is the number of queries, due to sorting word frequencies and querying each. Space complexity is O(N) for storing word frequency counts.

What Interviewers Usually Probe

  • Do you precompute any values for words to avoid repeated work?
  • Can you explain how array scanning and frequency counting connect to the hash lookup?
  • How would you handle very large queries efficiently without scanning words repeatedly?

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to count only strictly greater frequencies for queries.
  • Recomputing f(W) for each query instead of precomputing and reusing.
  • Mishandling edge cases where multiple characters tie as the smallest in a word.

Follow-up variants

  • Return counts where f(queries[i]) <= f(W) instead of strictly less than.
  • Find the maximum difference f(W) - f(query) for each query.
  • Handle uppercase and lowercase letters with same frequency comparison logic.

How GhostInterview Helps

  • Automatically precomputes and sorts word frequencies to reduce repeated scanning overhead.
  • Generates correct query comparison counts using array scanning plus hash lookup pattern.
  • Highlights edge cases in smallest character frequency counting and ensures efficient handling for all inputs.

Topic Pages

FAQ

How do I calculate f(s) for a string?

Scan the string to find the lexicographically smallest character and count its occurrences; this gives f(s).

Why precompute word frequencies instead of comparing each query directly?

Precomputing and sorting frequencies allows O(logN) comparisons per query instead of scanning all words repeatedly, improving efficiency.

Can I use a hash map instead of sorting?

Yes, a hash map of frequency counts works, but sorting simplifies counting how many words exceed a given frequency and aligns with the array scanning plus hash lookup pattern.

What if multiple characters are tied for smallest in a string?

Pick the lexicographically smallest character among them and count only its frequency for f(s).

Does this approach handle the LeetCode problem Compare Strings by Frequency of the Smallest Character efficiently?

Yes, by precomputing word frequencies and using binary search or scanning for queries, it ensures fast and correct results across all test cases.

GhostInterview Solver

Need direct help with Compare Strings by Frequency of the Smallest Character instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Compare Strings by Frequency of the Smallest Character from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.