Show problems as
Classic problem #3838EasyArrayStringSimulation

Weighted Word Mapping

Real-world version: Heaviest Label by Letter Weight
Tracked in this browser only
Read Weighted Word Mapping on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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