All problems
0928EasyArrayStringSimulation

Heaviest Label by Letter Weight

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3838Weighted Word Mapping

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A weight is given for every letter of the alphabet: weights[0] for 'a', weights[1] for 'b' and so on to weights[25] for 'z'.

A label's weight is the total of its letters' weights, counting a repeated letter every time it appears.

Return the heaviest label in words. When two labels weigh the same, return whichever is listed first.

Examples

Example 1

Input
words = ["fig", "kiwi", "melon", "plum"], weights = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3]
Output
"melon"

Adding up each label's letter weights, the heaviest of the four is the one returned.

Example 2

Input
words = ["cab", "bac", "abc"], weights = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Output
"cab"

Every letter weighs one, so all three labels weigh three, and the first listed wins.

Example 3

Input
words = ["aa", "zz"], weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
Output
"zz"

With weights rising through the alphabet, two z's weigh 52 against two a's at 2, so the second label wins even though it is listed later.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • weights.length == 26
  • 1 <= weights[i] <= 100
  • Every entry of words consists of lowercase English letters only

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def map_word_weights(words: list[str], weights: list[int]) -> str:
Java
public String mapWordWeights(String[] words, int[] weights)
September 7
Apply