Maximum Score Words Formed by Letters LeetCode solution in Python

1
class Solution:
2
def maxScoreWords(
3
self, words: List[str], letters: List[str], score: List[int]
4
) -> int:
5

6
f, ans = lambda x: sum(score[ord(c) - 97] for c in x), 0
7

8
def dfs(words, letters, tally):
9
nonlocal ans
10

11
for i in range(len(words)):
12
cWord = Counter(words[i])
13

14
if all(letters[c] >= cWord[c] for c in cWord):
15
dfs(words[i + 1 :], letters - cWord, tally + f(words[i]))
16

17
ans = max(ans, tally)
18
return ans
19

20
return dfs(words, Counter(letters), 0)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0