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 Formed by Letters INPUT Words: "dog" "cat" "dad" "good" Letters: a a c d d d g o o Scores (relevant): a=1 c=9 d=5 g=3 o=2 Word Scores: "dog" = 5+2+3 = 10 "cat" = 9+1+0 = 10 "dad" = 5+1+5 = 11 "good" = 3+2+2+5 = 12 n = 4 words (2^4 = 16 subsets) ALGORITHM STEPS 1 Count Available Letters Build frequency map from letters[] 2 Generate All Subsets Use bitmask: 0 to 2^n - 1 3 Validate Each Subset Check if letters suffice 4 Track Maximum Score Update max for valid subsets Key Subsets Explored: {dad,good} = 23 {dog,cat} = 20 {cat,dad} = 21 {good} = 12 Best: {dad, good} with letters d,a,d,g,o,o,d FINAL RESULT Optimal Word Selection: "dad" Score: 11 + "good" Score: 12 = Maximum Score 23 Letters Used: d a d g o o d Verification: "dad": 5+1+5 = 11 "good": 3+2+2+5 = 12 Key Insight: With only n=4 words, we can explore all 2^4=16 subsets using bitmask enumeration. For each subset, validate if available letters can form all words, then calculate total score. Time: O(2^n * m) where m is total characters. This brute-force approach works well for small n. TutorialsPoint - Maximum Score Words Formed by Letters | Optimal Solution (Bitmask)
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