Maximum Score Words Formed by Letters - Problem

Given a list of words, a list of single letters (might be repeating), and the score of every character, return the maximum score of any valid set of words formed by using the given letters.

Each word in words[i] cannot be used two or more times. It is not necessary to use all characters in letters and each letter can only be used once.

Score of letters 'a', 'b', 'c', ..., 'z' is given by score[0], score[1], ..., score[25] respectively.

Input & Output

Example 1 — Basic Word Selection
$ 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 total score of 11 + 12 = 23
Example 2 — Single Word Optimal
$ 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
💡 Note: We can form "xxxz" using letters x,x,x,z for score 5+5+5+10 = 25. Other combinations score less.
Example 3 — No Valid Words
$ 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 3 e's but only have 1, so return 0

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.

Visualization

Tap to expand
Maximum Score Words ProblemAvailable Letters:a(2), c(1), d(3), g(1), o(2)Word Scores:a=1, c=9, d=5, g=3, o=2Words to Consider:"dog"d+o+g=10"cat"c+a+t=10"dad"d+a+d=11→ Check Combinations →Best Choice"dad" = 11 ptsMaximum Score: 11Try all valid combinations and pick the highest score
Understanding the Visualization
1
Input
words=["dog","cat","dad"], letters=["a","a","c","d","d","d","g","o"], scores
2
Process
Try all combinations, check letter availability, calculate scores
3
Output
Return maximum score from valid word combinations
Key Takeaway
🎯 Key Insight: Use backtracking to systematically try all word combinations while efficiently tracking letter usage
Asked in
Google 12 Amazon 8 Facebook 6
28.5K Views
Medium Frequency
~25 min Avg. Time
892 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