Maximum Score Words Formed by Letters Problem
Description
LeetCode Problem 1255.
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 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.
Example 1:
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.
Example 2:
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.
Example 3:
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 <= 141 <= words[i].length <= 151 <= letters.length <= 100letters[i].length == 1score.length == 260 <= score[i] <= 10words[i],letters[i]contains only lower case English letters.
Solution
Approach: Backtracking over Take or Skip
There are at most 14 words, so we can afford to look at every subset of them (214 = 16384). The only rule is that together the chosen words must not use any letter more often than it appears in letters.
Count the available letters in left[26] and precompute each word's score. Then best(i) returns the highest score we can get from words[i..] with the letters still in left:
- Skip
words[i]:best(i + 1). - Take
words[i]: subtract its letters fromleft. If no count went negative, the word fits, andscore(words[i]) + best(i + 1)is a candidate. Then add the letters back so the caller seesleftunchanged.
The answer is best(0). Restoring the counts after the take branch is what makes the shared left array safe to reuse. A word that does not fit is never explored further, so most of the 214 subsets are cut off early when the letters are scarce. In Example 1, taking "dad" and "good" uses d three times, a once, g once and o twice, all within the bank, for 23 points.
Complexity: O(2n · L) time in the worst case, where n is the number of words and L the maximum word length, and O(n) space for the recursion plus 26 counters.
class Solution:
def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int:
left = [0] * 26 # letters still available
for c in letters:
left[ord(c) - ord("a")] += 1
word_score = [sum(score[ord(c) - ord("a")] for c in w) for w in words]
def best(i: int) -> int:
"""The best score from words[i:] with the letters in left."""
if i == len(words):
return 0
result = best(i + 1) # skip words[i]
fits = True
for c in words[i]:
left[ord(c) - ord("a")] -= 1
if left[ord(c) - ord("a")] < 0:
fits = False
if fits:
result = max(result, word_score[i] + best(i + 1)) # take words[i]
for c in words[i]:
left[ord(c) - ord("a")] += 1
return result
return best(0)