Weighted Word Mapping
Real-world version: Heaviest Label by Letter WeightThe 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.
def map_word_weights(words: list[str], weights: list[int]) -> str:public String mapWordWeights(String[] words, int[] weights)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Final Value of Variable After Performing OperationsReal-world version: Hand Tally Reading After The LogEasy
- Text JustificationReal-world version: Notice Board JustificationHard
- Longest Common PrefixReal-world version: Shared Crate CodeEasy
- Concatenation of ArrayReal-world version: Two Cycle PlayoutEasy