Weighted Word Mapping
Problem Descriptionโ
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 -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 (, , ..., ).
Return a string formed by concatenating the mapped characters for all words in order.
Example 1:โ
Input: words = ["abcd","def","xyz"]
weights = [5,3,12,14,1,2,3,2,10,6,6,9,7,8,7,10,8,9,6,9,9,8,3,7,7,2]
Output: "rij"
Explanation: - The weight of "abcd" is . The result modulo 26 is , which maps to 'r'.
- The weight of
"def"is . The result modulo 26 is , which maps to'i'. - The weight of
"xyz"is . The result modulo 26 is , which maps to'j'. Thus, the string formed by concatenating the mapped characters is"rij".
Video Explanationโ

Intuition & Approachโ
The problem asks us to simulate a specific mapping process for each word. The process is straightforward:
- Iterate through each word and calculate its total weight by looking up the value of each character in the
weightsarray. - Find the remainder of the total weight when divided by 26 (
total_weight % 26). - Map this remainder to a character in reverse alphabetical order. Since
'z'corresponds to 0,'y'to 1, etc., we can simply subtract the remainder from the ASCII value of'z'and convert it back to a character. - Append the mapped character to the final result string.
Complexityโ
- Time Complexity: , where is the total number of characters across all words in the array. We visit each character exactly once to calculate the sum.
- Space Complexity: , where is the number of words. The output string will contain exactly one character per word. Auxiliary space is .
Solutionsโ
- C++
- Java
- Python
- JavaScript
class Solution {
public:
string mapWordWeights(vector<string>& words, vector<int>& weights) {
string result = "";
for (const string& word : words) {
int sum = 0;
for (char c : word) {
sum = (sum + weights[c - 'a']) % 26;
}
int rem = sum;
result += (char)('z' - rem);
}
return result;
}
};
class Solution {
public String mapWordWeights(String[] words, int[] weights) {
StringBuilder result = new StringBuilder();
for (String word : words) {
int sum = 0;
for (char c : word.toCharArray()) {
sum = (sum + weights[c - 'a']) % 26;
}
int rem = sum;
result.append((char)('z' - rem));
}
return result.toString();
}
}
class Solution:
def mapWordWeights(self, words: list[str], weights: list[int]) -> str:
result = []
for word in words:
total_weight = sum(weights[ord(c) - ord('a')] for c in word)
rem = total_weight % 26
result.append(chr(ord('z') - rem))
return "".join(result)
/**
* @param {string[]} words
* @param {number[]} weights
* @return {string}
*/
var mapWordWeights = function(words, weights) {
let result = "";
for (let word of words) {
let sum = 0;
for (let char of word) {
sum = (sum + weights[char.charCodeAt(0) - 97]) % 26;
}
let rem = sum;
result += String.fromCharCode(122 - rem); // 'z' is 122
}
return result;
};
Done with this topic? Mark it as complete to track your progress.
Related Practice Problems
Handpicked problems sharing similar algorithmic topic tags
Partition Array According to Given Pivot
Rearranging an array based on a pivot value while maintaining the stable relative order of elements.
Process String with Special Operations I
Simulating string processing with backspace, duplicate, and reverse operations.
Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
๐ฌ Discuss this page
Have a question or spot something confusing in "Weighted Word Mapping"? Ask below โ it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.