Leetcode 1170: Compare Strings by Frequency of the Smallest Character

grid47
grid47
Exploring patterns and algorithms
Jul 13, 2024 7 min read

Define a function f(s) as the frequency of the lexicographically smallest character in a non-empty string s. For example, for s = 'abc', f(s) = 1 because the smallest character is 'a', appearing once. Given an array of strings queries and another array words, return an array where each entry corresponds to the number of strings in words where f(W) is greater than f(queries[i]) for a given i.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays of strings: `queries` and `words`.
Example: Input: queries = ["xyz", "aac"], words = ["aaa", "bbc", "cccc"]
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.
Output: An array of integers representing the count of strings in `words` such that `f(W) > f(queries[i])` for each query.
Example: Output: [3, 2]
Constraints:
Goal: Calculate `f(s)` for each string in `queries` and `words`, then compare the values to determine counts.
Steps:
• Calculate `f(W)` for all strings in `words` and store the results in a sorted array.
• For each query in `queries`, compute `f(queries[i])`.
• Use binary search on the sorted `words` array to count the number of `f(W)` values greater than `f(queries[i])`.
Goal: Constraints on input sizes and character types are provided.
Steps:
• 1 <= queries.length <= 2000
• 1 <= words.length <= 2000
• 1 <= queries[i].length, words[i].length <= 10
Assumptions:
• All strings are non-empty and contain only lowercase English letters.
Input: Input: queries = ["abc", "dd"], words = ["a", "bbb", "cccc"]
Explanation: For query 'abc', `f(abc) = 1`, and the counts of words with higher `f` are 2 (`bbb`, `cccc`). For query 'dd', `f(dd) = 2`, and only `cccc` has a higher `f`.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus