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
wordsthat you can potentially form - A collection of available
letters(some letters may appear multiple times) - A
scorearray wherescore[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
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
โ Quadratic Growth
Space Complexity
O(n + 26)
Recursion stack depth O(n) plus letter frequency array O(26)
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code