比昨天的簡單
昨天的好難
我好爛==
def maxScoreWords(self, words: List[str], letters: List[str], score:
List[int]) -> int:
ans = 0
letters_cnt = [letters.count(chr(i)) for i in range(ord('a'), ord('z')+1)]
n = len(words)
def dfs(idx, score_cur):
nonlocal ans
if idx == n:
ans = max(score_cur, ans)
return
if all([ words[idx].count(c) <= letters_cnt[ord(c)-ord('a')] for c in
words[idx] ]):
score_add = sum([score[ord(c) - ord('a')] for c in words[idx]])
score_cur += score_add
for c in words[idx]:
letters_cnt[ord(c) - ord('a')] -= 1
dfs(idx+1, score_cur)
score_cur -= score_add
for c in words[idx]:
letters_cnt[ord(c) - ord('a')] += 1
dfs(idx+1, score_cur)
dfs(0, 0)
return ans