Ctrl K

Best Word Set From Tiles

Hard
You are playing a word game. You hold letter tiles tiles (letters may repeat), and each tile can be used at most once. There is a list of words words; each entry of the list can be laid out at most once. Letter values come from points, an array of 26 numbers: points[0] is the value of a, points[1] of b, and so on up to z. A word is worth the sum of its letters' values. Choose a set of words that can all be built together from your tiles so that their total value is as large as possible, and return that value. You do not have to use every tile; an empty set is worth 0.
Example 1
words=['bee', 'tea', 'eat', 'beet'], tiles=['b', 'e', 'e', 't', 'a'], points=[1, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0] → 7

Points: a=1, b=3, e=1, t=2. "beet" scores 3+1+1+2 = 7 and uses every tile except "a". "tea" and "eat" cannot both be built since there is only one "t", and each alone is worth just 4.

Example 2
words=['ab', 'ab'], tiles=['a', 'b', 'a', 'b'], points=[2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] → 14

These are two separate entries of the list and there are enough tiles for both: 2 · (2 + 5) = 14.

Example 3
words=['zz'], tiles=['z'], points=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10] → 0

"zz" needs two "z" tiles but there is only one. No word can be laid out, so the answer is 0.

Constraints
  • 1 ≤ len(words) ≤ 14
  • 1 ≤ len(words[i]) ≤ 15
  • 1 ≤ len(tiles) ≤ 100
  • each element of tiles is a single letter
  • len(points) = 26
  • 0 ≤ points[i] ≤ 10
  • words[i] and tiles[i] consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSettle Up in the Fewest Transfers HardAssembling Fragments HardSquare-Sum Chains HardOrders of Identical Parts HardSharing Jobs Fairly Hard
loading Python…
Input
words=['bee', 'tea', 'eat', 'beet'], tiles=['b', 'e', 'e', 't', 'a'], points=[1, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 54 tests on the server, hidden ones included.