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".
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++β
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;
}
};
Javaβ
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();
}
}
Pythonβ
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)
JavaScriptβ
/**
* @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;
};