# LeetCode: 1255. 得分最高的单词集合¶

## 1、题目描述¶

• 玩家需要用字母表 letters 里的字母来拼写单词表 words 中的单词。
• 可以只使用字母表 letters 中的部分字母，但是每个字母最多被使用一次。
• 单词表 words 中每个单词只能计分（使用）一次。
• 根据字母得分情况表score，字母 'a', 'b', 'c', ... , 'z' 对应的得分分别为 score[0], score[1], ..., score[25]
• 本场游戏的「得分」是指：玩家所拼写出的单词集合里包含的所有字母的得分之和。

输入：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]



输入：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]



输入：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]



• $1 <= words.length <= 14$
• $1 <= words[i].length <= 15$
• $1 <= letters.length <= 100$
• $letters[i].length == 1$
• $score.length == 26$
• $0 <= score[i] <= 10$
• words[i]letters[i] 只包含小写的英文字母。

## 2、解题思路¶

• 简单dfs，每个单词都有两种状态，用当前单词和不用当前单词，遍历到最后即可
from collections import Counter

class Solution:
def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int:

def get_score(le_count):
return sum([score[ord(l) - ord('a')] * num for l, num in le_count.items()])

length = len(words)
letter_count = Counter(letters)
total_count = get_score(letter_count)
ans = 0

def dfs(count, pos):
nonlocal ans
if pos >= length:
ans = max(ans, total_count - get_score(count))
return
if not (Counter(words[pos]) - count):
dfs(count - Counter(words[pos]), pos + 1)
dfs(count, pos + 1)

dfs(letter_count, 0)

return ans