[LeetCode] Weighted Word Mapping

3838. Weighted Word Mapping

You are given an array of strings words, where each string represents a word containing lowercase English letters.

You are also given an integer array weights of length 26, where weights[i] represents the weight of the i^th lowercase English letter.

The weight of a word is defined as the sum of the weights of its characters.

For each word, take its weight modulo 26 and map the result to a lowercase English letter using reverse alphabetical order (0 -> 'z', 1 -> 'y', ..., 25 -> 'a').

Return a string formed by concatenating the mapped characters for all words in order.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string mapWordWeights(vector<string>& words, vector<int>& weights) {
string res = "";
for(auto& w : words) {
int sum = 0;
for(auto& ch : w) sum += weights[ch-'a'];
res.push_back('z' - sum % 26);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/weighted-word-mapping/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.