LeetCode 题解工作台
得分最高的单词集合
你将会得到一份单词表 words ,一个字母表 letters (可能会有重复字母),以及每个字母对应的得分情况表 score 。 请你帮忙计算玩家在单词拼写游戏中所能获得的「最高得分」:能够由 letters 里的字母拼写出的 任意 属于 words 单词子集中,分数最高的单词集合的得分。 单词拼…
6
题型
4
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们注意到题目的数据范围不大,因此对于给定的单词表,我们可以使用二进制枚举的方法,枚举出所有的单词组合,然后判断每个单词组合是否满足题目要求,如果满足则计算其得分,最后取得分最大的单词组合。 我们首先用哈希表或数组 记录字母表 中每个字母出现的次数。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
你将会得到一份单词表 words,一个字母表 letters (可能会有重复字母),以及每个字母对应的得分情况表 score。
请你帮忙计算玩家在单词拼写游戏中所能获得的「最高得分」:能够由 letters 里的字母拼写出的 任意 属于 words 单词子集中,分数最高的单词集合的得分。
单词拼写游戏的规则概述如下:
- 玩家需要用字母表
letters里的字母来拼写单词表words中的单词。 - 可以只使用字母表
letters中的部分字母,但是每个字母最多被使用一次。 - 单词表
words中每个单词只能计分(使用)一次。 - 根据字母得分情况表
score,字母'a','b','c', ... ,'z'对应的得分分别为score[0],score[1], ...,score[25]。 - 本场游戏的「得分」是指:玩家所拼写出的单词集合里包含的所有字母的得分之和。
示例 1:
输入:words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"], score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0] 输出:23 解释: 字母得分为 a=1, c=9, d=5, g=3, o=2 使用给定的字母表 letters,我们可以拼写单词 "dad" (5+1+5)和 "good" (3+2+2+5),得分为 23 。 而单词 "dad" 和 "dog" 只能得到 21 分。
示例 2:
输入:words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"], score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10] 输出:27 解释: 字母得分为 a=4, b=4, c=4, x=5, z=10 使用给定的字母表 letters,我们可以组成单词 "ax" (4+5), "bx" (4+5) 和 "cx" (4+5) ,总得分为 27 。 单词 "xxxz" 的得分仅为 25 。
示例 3:
输入:words = ["leetcode"], letters = ["l","e","t","c","o","d"], score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0] 输出:0 解释: 字母 "e" 在字母表 letters 中只出现了一次,所以无法组成单词表 words 中的单词。
提示:
1 <= words.length <= 141 <= words[i].length <= 151 <= letters.length <= 100letters[i].length == 1score.length == 260 <= score[i] <= 10words[i]和letters[i]只包含小写的英文字母。
解题思路
方法一:二进制枚举
我们注意到题目的数据范围不大,因此对于给定的单词表,我们可以使用二进制枚举的方法,枚举出所有的单词组合,然后判断每个单词组合是否满足题目要求,如果满足则计算其得分,最后取得分最大的单词组合。
我们首先用哈希表或数组 记录字母表 中每个字母出现的次数。
接下来,我们使用二进制枚举的方法,枚举出所有的单词组合。二进制的每一位表示单词表中的每一个单词是否被选中,如果第 位为 ,则表示第 个单词被选中,否则表示第 个单词没有被选中。
然后我们统计当前单词组合中每个字母出现的次数,记录在哈希表或数组 中。如果 中的每个字母的出现次数都不大于 中的对应字母的出现次数,则说明当前单词组合满足题目要求,我们计算当前单词组合的得分,取得分最大的单词组合。
时间复杂度 ,空间复杂度 。其中 和 分别为单词集合中单词的个数和单词的最大长度;而 为字母表中字母的个数,本题中 。
class Solution:
def maxScoreWords(
self, words: List[str], letters: List[str], score: List[int]
) -> int:
cnt = Counter(letters)
n = len(words)
ans = 0
for i in range(1 << n):
cur = Counter(''.join([words[j] for j in range(n) if i >> j & 1]))
if all(v <= cnt[c] for c, v in cur.items()):
t = sum(v * score[ord(c) - ord('a')] for c, v in cur.items())
ans = max(ans, t)
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(2^W * (L + A)) where W is the number of words, L is the maximum word length, and A is the alphabet size (26). Space complexity is O(A + W) to store letter counts and DP cache for state transitions. |
| 空间 | O(A + W) |
面试官常问的追问
外企场景- question_mark
Look for state transition DP usage over naive backtracking.
- question_mark
Check how efficiently the candidate handles word feasibility checks using letter counts.
- question_mark
Observe if the candidate considers pruning invalid subsets to avoid redundant recursion.
常见陷阱
外企场景- error
Attempting to generate all permutations of letters instead of word subsets, causing exponential blowup.
- error
Failing to handle repeated letters correctly, leading to invalid word formation.
- error
Not caching intermediate results, which results in unnecessary recomputation of subset scores.
进阶变体
外企场景- arrow_right_alt
Max score with repeated words allowed, adjusting DP accordingly.
- arrow_right_alt
Words have additional constraints like mandatory inclusion of specific letters.
- arrow_right_alt
Score function changes to include multipliers or penalties for certain letters.