LeetCode 题解工作台

比较字符串最小字母出现频次

定义一个函数 f(s) ,统计 s 中 (按字典序比较)最小字母的出现频次 ,其中 s 是一个非空字符串。 例如,若 s = "dcce" ,那么 f(s) = 2 ,因为字典序最小字母是 "c" ,它出现了 2 次。 现在,给你两个字符串数组待查表 queries 和词汇表 words 。对于每次…

category

5

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们先按照题目描述,实现函数 ,函数返回字符串 中按字典序比较最小字母的出现频次。 接下来,我们将 中的每个字符串 都计算出 ,并将其排序,存放在数组 中。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

定义一个函数 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 <= 2000
  • 1 <= words.length <= 2000
  • 1 <= queries[i].length, words[i].length <= 10
  • queries[i][j]words[i][j] 都由小写英文字母组成
lightbulb

解题思路

方法一:排序 + 二分查找

我们先按照题目描述,实现函数 f(s)f(s),函数返回字符串 ss 中按字典序比较最小字母的出现频次。

接下来,我们将 wordswords 中的每个字符串 ww 都计算出 f(w)f(w),并将其排序,存放在数组 numsnums 中。

然后,我们遍历 queriesqueries 中的每个字符串 qq,在 numsnums 中二分查找第一个大于 f(q)f(q) 的位置 ii,则 numsnums 中下标 ii 及其后面的元素都满足 f(q)<f(W)f(q) < f(W),那么当前查询的答案就是 nin - i

时间复杂度 O((n+q)×M)O((n + q) \times M),空间复杂度 O(n)O(n)。其中 nnqq 分别是数组 wordswordsqueriesqueries 的长度,而 MM 是字符串的最大长度。

1
2
3
4
5
6
7
8
9
10
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]
speed

复杂度分析

指标
时间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
psychology

面试官常问的追问

外企场景
  • 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?

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

比较字符串最小字母出现频次题解:数组·哈希·扫描 | LeetCode #1170 中等