Maximum Score Words Formed by Letters - Problem

Imagine you're playing a strategic word game where you need to maximize your score by selecting the best combination of words!

You're given:

  • A list of words that you can potentially form
  • A collection of available letters (some letters may appear multiple times)
  • A score array where score[0] represents the points for 'a', score[1] for 'b', and so on

Your goal is to find the maximum possible score by selecting a subset of words that can be formed using the available letters. Remember:

  • Each word can only be used once
  • Each letter can only be used once
  • You don't have to use all available letters
  • You don't have to form all possible words

The challenge is to find the optimal combination that gives you the highest total score!

Input & Output

example_1.py โ€” Basic Case
$ 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
๐Ÿ’ก Note: We can form "dad" (d=5, a=1, d=5) and "good" (g=3, o=2, o=2, d=5) for a total score of 11 + 12 = 23. Note that we have enough letters: we have 2 'a's and need 1, we have 3 'd's and need 2, etc.
example_2.py โ€” All Words Case
$ 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,1,0,2]
โ€บ Output: 27
๐Ÿ’ก Note: We can form all words: "xxxz" (score: 1+1+1+2=5), "ax" (score: 4+1=5), "bx" (score: 4+1=5), "cx" (score: 4+1=5). We have exactly the right letters: 3 x's for xxxz, 1 x each for ax/bx/cx, and the required a,b,c,z.
example_3.py โ€” No Words Case
$ 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
๐Ÿ’ก Note: We cannot form "leetcode" because we need 2 'e's but only have 1 'e' available. Since we can't form any words, the maximum score is 0.

Visualization

Tap to expand
Letter Budget ManagementAvailable Lettersa:2, d:3, g:1, o:2, c:1Budget: 9 letters totalWord: "dad"Cost: d:2, a:1Word: "good"Cost: g:1, o:2, d:1After "dad"a:1, d:1, g:1, o:2, c:1Budget: 6 letters leftAfter "good" tooa:1, d:0, g:0, o:0, c:1Budget: 2 letters leftFinal Score23 pointsdad(11) + good(12)๐Ÿ”„ Backtracking: After exploring "take dad + good", we restorethe original letter counts and try "skip dad" branchKey Insight: Systematic exploration with state restorationโ€ข Try including each word if we have enough lettersโ€ข Backtrack by restoring letter counts after each exploration
Understanding the Visualization
1
Initialize
Count available letters and start with first word
2
Decision
For each word: can we afford it with current letters?
3
Spend & Recurse
If yes, spend the letters and explore remaining words
4
Backtrack
Restore spent letters and try skipping this word
5
Maximize
Return the maximum score from both choices
Key Takeaway
๐ŸŽฏ Key Insight: Backtracking with pruning allows us to systematically explore all valid word combinations while avoiding impossible branches early, making it much more efficient than brute force enumeration.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n)

In worst case we still explore all combinations, but pruning makes it much faster in practice

n
2n
โš  Quadratic Growth
Space Complexity
O(n + 26)

Recursion stack depth O(n) plus letter frequency array O(26)

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค words.length โ‰ค 14
  • 1 โ‰ค words[i].length โ‰ค 15
  • 1 โ‰ค letters.length โ‰ค 100
  • letters[i].length == 1
  • score.length == 26
  • 0 โ‰ค score[i] โ‰ค 104
  • words[i], letters[i] contains only lower case English letters
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 22
89.3K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen