> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Score Words Formed by Letters

> Tested Python solution for LeetCode 1255 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1255, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Dynamic Programming](/catalog/topics/dynamic-programming), [Backtracking](/catalog/topics/backtracking), [Bit Manipulation](/catalog/topics/bit-manipulation), [Counting](/catalog/topics/counting), [Bitmask](/catalog/topics/bitmask). [View on LeetCode](https://leetcode.com/problems/maximum-score-words-formed-by-letters/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1255   # by problem number
lcpy gen -s maximum_score_words_formed_by_letters   # by problem name
```

## Problem

Given a list of words, list of  single letters (might be repeating) and score of every character.

Return the maximum score of any valid set of words formed by using the given letters (words\[i] cannot be used two or more times).

It is not necessary to use all characters in \<code>letters\</code> and each letter can only be used once. Score of letters \<code>'a'\</code>, \<code>'b'\</code>, \<code>'c'\</code>, ... ,\<code>'z'\</code> is given by \<code>score\[0]\</code>, \<code>score\[1]\</code>, ... , \<code>score\[25]\</code> respectively.

### Examples

```
Input: 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]
Output: 23
Explanation:
Score  a=1, c=9, d=5, g=3, o=2
Given letters, we can form the words "dad" (5+1+5) and "good" (3+2+2+5) with a score of 23.
Words "dad" and "dog" only get a score of 21.
```

```
Input: 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]
Output: 27
Explanation:
Score  a=4, b=4, c=4, x=5, z=10
Given letters, we can form the words "ax" (4+5), "bx" (4+5) and "cx" (4+5) with a score of 27.
Word "xxxz" only get a score of 25.
```

```
Input: 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]
Output: 0
Explanation:
Letter "e" can only be used once.
```

### Constraints

* 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] contains only lower case English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_words_formed_by_letters/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_words_formed_by_letters/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    def max_score_words(self, words: list[str], letters: list[str], score: list[int]) -> int:
        word_counts = [Counter(word) for word in words]
        word_scores = [
            sum(score[ord(c) - 97] * cnt for c, cnt in Counter(word).items()) for word in words
        ]
        n = len(words)

        def backtrack(i: int, available: Counter) -> int:
            if i == n:
                return 0
            best = backtrack(i + 1, available)
            counts = word_counts[i]
            if all(available[c] >= cnt for c, cnt in counts.items()):
                for c, cnt in counts.items():
                    available[c] -= cnt
                best = max(best, word_scores[i] + backtrack(i + 1, available))
                for c, cnt in counts.items():
                    available[c] += cnt
            return best

        return backtrack(0, Counter(letters))
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.