LeetCode 题解工作台
比较字符串最小字母出现频次
定义一个函数 f(s) ,统计 s 中 (按字典序比较)最小字母的出现频次 ,其中 s 是一个非空字符串。 例如,若 s = "dcce" ,那么 f(s) = 2 ,因为字典序最小字母是 "c" ,它出现了 2 次。 现在,给你两个字符串数组待查表 queries 和词汇表 words 。对于每次…
5
题型
5
代码语言
3
相关题
当前训练重点
中等 · 数组·哈希·扫描
答案摘要
我们先按照题目描述,实现函数 ,函数返回字符串 中按字典序比较最小字母的出现频次。 接下来,我们将 中的每个字符串 都计算出 ,并将其排序,存放在数组 中。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
定义一个函数 f(s),统计 s 中(按字典序比较)最小字母的出现频次 ,其中 s 是一个非空字符串。
例如,若 s = "dcce",那么 f(s) = 2,因为字典序最小字母是 "c",它出现了 2 次。
现在,给你两个字符串数组待查表 queries 和词汇表 words 。对于每次查询 queries[i] ,需统计 words 中满足 f(queries[i]) < f(W) 的 词的数目 ,W 表示词汇表 words 中的每个词。
请你返回一个整数数组 answer 作为答案,其中每个 answer[i] 是第 i 次查询的结果。
示例 1:
输入:queries = ["cbd"], words = ["zaaaz"]
输出:[1]
解释:查询 f("cbd") = 1,而 f("zaaaz") = 3 所以 f("cbd") < f("zaaaz")。
示例 2:
输入:queries = ["bbb","cc"], words = ["a","aa","aaa","aaaa"]
输出:[1,2]
解释:第一个查询 f("bbb") < f("aaaa"),第二个查询 f("aaa") 和 f("aaaa") 都 > f("cc")。
提示:
1 <= queries.length <= 20001 <= words.length <= 20001 <= queries[i].length, words[i].length <= 10queries[i][j]、words[i][j]都由小写英文字母组成
解题思路
方法一:排序 + 二分查找
我们先按照题目描述,实现函数 ,函数返回字符串 中按字典序比较最小字母的出现频次。
接下来,我们将 中的每个字符串 都计算出 ,并将其排序,存放在数组 中。
然后,我们遍历 中的每个字符串 ,在 中二分查找第一个大于 的位置 ,则 中下标 及其后面的元素都满足 ,那么当前查询的答案就是 。
时间复杂度 ,空间复杂度 。其中 和 分别是数组 和 的长度,而 是字符串的最大长度。
class Solution:
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
def f(s: str) -> int:
cnt = Counter(s)
return next(cnt[c] for c in ascii_lowercase if cnt[c])
n = len(words)
nums = sorted(f(w) for w in words)
return [n - bisect_right(nums, f(q)) for q in queries]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | 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. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Do you precompute any values for words to avoid repeated work?
- question_mark
Can you explain how array scanning and frequency counting connect to the hash lookup?
- question_mark
How would you handle very large queries efficiently without scanning words repeatedly?
常见陷阱
外企场景- error
Forgetting to count only strictly greater frequencies for queries.
- error
Recomputing f(W) for each query instead of precomputing and reusing.
- error
Mishandling edge cases where multiple characters tie as the smallest in a word.
进阶变体
外企场景- arrow_right_alt
Return counts where f(queries[i]) <= f(W) instead of strictly less than.
- arrow_right_alt
Find the maximum difference f(W) - f(query) for each query.
- arrow_right_alt
Handle uppercase and lowercase letters with same frequency comparison logic.