|
| 1 | +## 题目地址(1255. 得分最高的单词集合) |
| 2 | + |
| 3 | +https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/ |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +``` |
| 8 | +你将会得到一份单词表 words,一个字母表 letters (可能会有重复字母),以及每个字母对应的得分情况表 score。 |
| 9 | +
|
| 10 | +请你帮忙计算玩家在单词拼写游戏中所能获得的「最高得分」:能够由 letters 里的字母拼写出的 任意 属于 words 单词子集中,分数最高的单词集合的得分。 |
| 11 | +
|
| 12 | +单词拼写游戏的规则概述如下: |
| 13 | +
|
| 14 | +玩家需要用字母表 letters 里的字母来拼写单词表 words 中的单词。 |
| 15 | +可以只使用字母表 letters 中的部分字母,但是每个字母最多被使用一次。 |
| 16 | +单词表 words 中每个单词只能计分(使用)一次。 |
| 17 | +根据字母得分情况表score,字母 'a', 'b', 'c', ... , 'z' 对应的得分分别为 score[0], score[1], ..., score[25]。 |
| 18 | +本场游戏的「得分」是指:玩家所拼写出的单词集合里包含的所有字母的得分之和。 |
| 19 | + |
| 20 | +
|
| 21 | +示例 1: |
| 22 | +
|
| 23 | +输入: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] |
| 24 | +输出:23 |
| 25 | +解释: |
| 26 | +字母得分为 a=1, c=9, d=5, g=3, o=2 |
| 27 | +使用给定的字母表 letters,我们可以拼写单词 "dad" (5+1+5)和 "good" (3+2+2+5),得分为 23 。 |
| 28 | +而单词 "dad" 和 "dog" 只能得到 21 分。 |
| 29 | +示例 2: |
| 30 | +
|
| 31 | +输入: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] |
| 32 | +输出:27 |
| 33 | +解释: |
| 34 | +字母得分为 a=4, b=4, c=4, x=5, z=10 |
| 35 | +使用给定的字母表 letters,我们可以组成单词 "ax" (4+5), "bx" (4+5) 和 "cx" (4+5) ,总得分为 27 。 |
| 36 | +单词 "xxxz" 的得分仅为 25 。 |
| 37 | +示例 3: |
| 38 | +
|
| 39 | +输入: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] |
| 40 | +输出:0 |
| 41 | +解释: |
| 42 | +字母 "e" 在字母表 letters 中只出现了一次,所以无法组成单词表 words 中的单词。 |
| 43 | + |
| 44 | +
|
| 45 | +提示: |
| 46 | +
|
| 47 | +1 <= words.length <= 14 |
| 48 | +1 <= words[i].length <= 15 |
| 49 | +1 <= letters.length <= 100 |
| 50 | +letters[i].length == 1 |
| 51 | +score.length == 26 |
| 52 | +0 <= score[i] <= 10 |
| 53 | +words[i] 和 letters[i] 只包含小写的英文字母。 |
| 54 | +
|
| 55 | +
|
| 56 | +``` |
| 57 | + |
| 58 | +## 前置知识 |
| 59 | + |
| 60 | +- 回溯 |
| 61 | + |
| 62 | +## 公司 |
| 63 | + |
| 64 | +- 暂无 |
| 65 | + |
| 66 | +## 思路 |
| 67 | + |
| 68 | +题目的本质就是枚举所有的 words 组合,然后判断是否可以满足单词拼写的游戏规则,最后找出所有满足条件的最大分数即可。因此这道题可以用到 [78. 子集](../problems/78.subsets.md) 的代码。 |
| 69 | + |
| 70 | +由排列组合原理可知, 一个大小为 N 的集合的组合数是 2^N,因此这种解法的时间复杂度也大致是这个量级。 |
| 71 | + |
| 72 | +这道题比[78. 子集](../problems/78.subsets.md) 稍微复杂一点,不管是题目的数据输入还是限制条件都更复杂。 |
| 73 | + |
| 74 | +实际上, 这些限制条件影响的只是部分细节,我们仍然套用回溯的模板即可, 关于回溯模板可参考:[回溯专题](../thinkings/backtrack.md)。 |
| 75 | + |
| 76 | +核心伪代码如下: |
| 77 | + |
| 78 | +```py |
| 79 | +class Solution: |
| 80 | + def maxScoreWords(self, words, letters, score): |
| 81 | + ans = 0 |
| 82 | + |
| 83 | + def dfs(start, 当前的分数, counter): |
| 84 | + if start > len(words): return |
| 85 | + ans = max(ans, cur) |
| 86 | + for j in 循环start之后的单词: |
| 87 | + if 如果当前单词加进去还满足游戏规则: |
| 88 | + dfs(j + 1, 新的分数, 新的counter) |
| 89 | + |
| 90 | + dfs(0, 0, collections.Counter(letters)) |
| 91 | + return ans |
| 92 | +``` |
| 93 | + |
| 94 | +> 由于每次都新生成一个 counter,因此状态不需要回溯。 |
| 95 | +
|
| 96 | +其中 collections.Counter(letters) 的功能是计数,比如['a', 'a', 'c', 'b'],会被处理为 { a: 2, b: 1, c: 1}。其功能是用于判断**当前单词加进去是否还满足游戏规则**。具体可以参考下方的代码区。 |
| 97 | + |
| 98 | +## 关键点 |
| 99 | + |
| 100 | +- 回溯模板 |
| 101 | +- 计数 |
| 102 | + |
| 103 | +## 代码 |
| 104 | + |
| 105 | +代码支持 Python3: |
| 106 | + |
| 107 | +Python3 Code: |
| 108 | + |
| 109 | +```python |
| 110 | +class Solution: |
| 111 | + def maxScoreWords(self, words, letters, score): |
| 112 | + self.ans = 0 |
| 113 | + words_score = [sum(score[ord(c)-ord('a')] for c in word) for word in words] |
| 114 | + words_counter = [collections.Counter(word) for word in words] |
| 115 | + |
| 116 | + def backtrack(start, cur, counter): |
| 117 | + if start > len(words): |
| 118 | + return |
| 119 | + self.ans = max(self.ans, cur) |
| 120 | + for j, w_counter in enumerate(words_counter[start:], start): |
| 121 | + if all(n <= counter.get(c,0) for c,n in w_counter.items()): |
| 122 | + backtrack(j+1, cur+words_score[j], counter-w_counter) |
| 123 | + |
| 124 | + backtrack(0, 0, collections.Counter(letters)) |
| 125 | + return self.ans |
| 126 | +``` |
| 127 | + |
| 128 | +**复杂度分析** |
| 129 | + |
| 130 | +- 时间复杂度:$O(2^N)$,其中 N 为 words 的个数。 |
| 131 | +- 空间复杂度:$O(total)$,其中 total 为 words 中的字符总数。 |
| 132 | + |
| 133 | +大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。 |
| 134 | +大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 |
| 135 | + |
0 commit comments