LeetCode link: 3838. Weighted Word Mapping, difficulty: Medium.
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.
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"is5 + 3 + 12 + 14 = 34. The result modulo 26 is34 % 26 = 8, which maps to'r'. - The weight of
"def"is14 + 1 + 2 = 17. The result modulo 26 is17 % 26 = 17, which maps to'i'. - The weight of
"xyz"is7 + 7 + 2 = 16. The result modulo 26 is16 % 26 = 16, which maps to'j'.
Thus, the string formed by concatenating the mapped characters is "rij".
Example 2:
Input: words = ["a","b","c"], 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: "yyy"
Explanation:
Each word has weight 1. The result modulo 26 is 1 % 26 = 1, which maps to 'y'.
Thus, the string formed by concatenating the mapped characters is "yyy".
Example 3:
Input: words = ["abcd"], weights = [7,5,3,4,3,5,4,9,4,2,2,7,10,2,5,10,6,1,2,2,4,1,3,4,4,5]
Output: "g"
Explanation:
The weight of "abcd" is 7 + 5 + 3 + 4 = 19. The result modulo 26 is 19 % 26 = 19, which maps to 'g'.
Thus, the string formed by concatenating the mapped characters is "g".
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 10weights.length == 261 <= weights[i] <= 100words[i]consists of lowercase English letters.
Topics
Hint 1
For each word, sum character weights using weights[c - 'a']
Hint 2
Take the sum modulo 26
Hint 3
Map the value to a character using reverse order: char = 'z' - value
Hint 4
Append all mapped characters in order to form the result string
Intuition
The problem is a straightforward simulation of mapping rules. For each string in the given words array, we need to calculate a "weight". This weight is the sum of the weights of all characters in the word, where the character's weight is determined by mapping its alphabetical index to the weights array.
After finding the sum, we apply the modulo 26 operation. Finally, we map this resulting value to a character in reverse alphabetical order, which mathematically corresponds to subtracting the value from the ASCII code of 'z' and converting it back to a character. We concatenate the resulting characters for all words and return the final string.
👋 Hi, I'm the developer behind coder.id — Build your programmer brand. Create your portfolio, resume, and blog in minutes, and land your dream offer!
Step-by-Step Solution
- Initialize an empty list or string builder to collect the mapped characters.
- Iterate through each
wordin thewordsarray. - For each
word, initialize asumto 0. Iterate through each charactercin the word, and addweights[c - 'a']tosum. - Take the calculated
summodulo 26 to get the mapped value. - Convert this value to a character using the reverse alphabetical rule:
char = 'z' - value. - Append the computed character to the string builder.
- After processing all words, return the concatenated string.
Complexity
Time complexity
O(N * M)
Space complexity
O(N * M)
Explanation
- Time Complexity:
O(N * M)We iterate through allNwords in the array, and for each word, we iterate through itsMcharacters. Thus, the time complexity is strictly proportional to the total number of characters across all words. - Space Complexity:
O(N)We use a string builder or array to store the resulting mapped character for each word. Since there areNwords, the resulting string will have lengthN. Thus, the auxiliary space isO(N).
Python #
class Solution:
def mapWordWeights(self, words: List[str], weights: List[int]) -> str:
# Array to store the mapped characters for each word
result = []
# Iterate over each word in the input list
for word in words:
# Calculate the sum of weights for the characters in the current word
word_weight = 0
for char in word:
# Get the weight of the character by mapping it to its index (0 for 'a', 1 for 'b', etc.)
word_weight += weights[ord(char) - ord('a')]
# Take the sum modulo 26 as specified in the problem
mapped_val = word_weight % 26
# Map the value to a character using reverse alphabetical order ('z' - value)
mapped_char = chr(ord('z') - mapped_val)
# Append the mapped character to the result list
result.append(mapped_char)
# Join the characters into a single string and return
return "".join(result)
Java #
class Solution {
public String mapWordWeights(String[] words, int[] weights) {
// Use a StringBuilder for efficient string concatenation
StringBuilder result = new StringBuilder();
// Iterate through each word in the array
for (String word : words) {
int wordWeight = 0;
// Sum the weights of all characters in the current word
for (char c : word.toCharArray()) {
// Determine the 0-indexed position of the character
wordWeight += weights[c - 'a'];
}
// Modulo 26 operation
int mappedVal = wordWeight % 26;
// Calculate the mapped character using reverse alphabetical order
// Since 'z' is the starting point (0 -> 'z'), we subtract the mapped value
char mappedChar = (char) ('z' - mappedVal);
// Append the mapped character to our result
result.append(mappedChar);
}
// Convert the StringBuilder to String and return
return result.toString();
}
}
JavaScript #
/**
* @param {string[]} words
* @param {number[]} weights
* @return {string}
*/
var mapWordWeights = function(words, weights) {
// Array to collect the resulting characters
let result = [];
// Process each word one by one
for (let word of words) {
let wordWeight = 0;
// Sum up the weight of each character in the word
for (let i = 0; i < word.length; i++) {
// Find the character code difference from 'a'
let charIndex = word.charCodeAt(i) - 97; // 97 is the ASCII for 'a'
wordWeight += weights[charIndex];
}
// Apply modulo 26 to the sum
let mappedVal = wordWeight % 26;
// Map it back to a character starting backwards from 'z' (ASCII 122)
let mappedChar = String.fromCharCode(122 - mappedVal);
// Push to the result array
result.push(mappedChar);
}
// Join the array into a string and return
return result.join('');
};
C++ #
class Solution {
public:
string mapWordWeights(vector<string>& words, vector<int>& weights) {
// String to store the concatenated mapped characters
string result = "";
// Process each word in the input
for (const string& word : words) {
int wordWeight = 0;
// Sum the weights corresponding to each character
for (char c : word) {
// Find the index by subtracting the ASCII value of 'a'
wordWeight += weights[c - 'a'];
}
// Perform the modulo 26 operation
int mappedVal = wordWeight % 26;
// Deduct the mapped value from 'z' to get the reversed mapped character
char mappedChar = 'z' - mappedVal;
// Append it to the result string
result += mappedChar;
}
return result;
}
};
C# #
public class Solution {
public string mapWordWeights(string[] words, int[] weights) {
// StringBuilder is optimal for creating strings character by character
System.Text.StringBuilder result = new System.Text.StringBuilder();
// Iterate over each string in words
foreach (string word in words) {
int wordWeight = 0;
// Calculate total weight of the word
foreach (char c in word) {
// c - 'a' yields the 0-based index of the alphabet
wordWeight += weights[c - 'a'];
}
// Modulo 26 ensures the value is between 0 and 25
int mappedVal = wordWeight % 26;
// Map the value in reverse order (0 -> 'z', 1 -> 'y', etc.)
char mappedChar = (char)('z' - mappedVal);
// Append the mapped character
result.Append(mappedChar);
}
// Return the final concatenated string
return result.ToString();
}
}
Go #
func mapWordWeights(words []string, weights []int) string {
// Use a byte slice to efficiently build the result string
result := make([]byte, 0, len(words))
// Iterate through all the words
for _, word := range words {
wordWeight := 0
// Iterate through each character of the word
for i := 0; i < len(word); i++ {
// Find character index (0-25) by subtracting ASCII of 'a'
charIndex := word[i] - 'a'
wordWeight += weights[charIndex]
}
// Take modulo 26
mappedVal := wordWeight % 26
// Reverse alphabetical mapping: 'z' - mappedVal
mappedChar := byte('z' - mappedVal)
// Append the character to our byte slice
result = append(result, mappedChar)
}
// Convert byte slice back to string
return string(result)
}
Ruby #
# @param {String[]} words
# @param {Integer[]} weights
# @return {String}
def map_word_weights(words, weights)
result = ""
# Process each word individually
words.each do |word|
word_weight = 0
# Iterate over each character's byte value
word.each_byte do |byte|
# byte of 'a' is 97, so we subtract 97 to get the index
char_index = byte - 97
word_weight += weights[char_index]
end
# Take the weight modulo 26
mapped_val = word_weight % 26
# Convert the reverse alphabetical mapped value back to a character
# 'z'.ord is 122
mapped_char = (122 - mapped_val).chr
# Append the character to the result string
result << mapped_char
end
result
end
Rust #
impl Solution {
pub fn map_word_weights(words: Vec<String>, weights: Vec<i32>) -> String {
// String to store our concatenated results
let mut result = String::with_capacity(words.len());
// Iterate over all words
for word in words {
let mut word_weight = 0;
// Calculate the total weight of the word
for c in word.bytes() {
// b'a' is the byte value of 'a'
let char_index = (c - b'a') as usize;
word_weight += weights[char_index];
}
// Modulo 26 operation
let mapped_val = word_weight % 26;
// Map the value to reverse alphabetical order (b'z' is 122)
let mapped_char = (b'z' - mapped_val as u8) as char;
// Push the character to our result string
result.push(mapped_char);
}
result
}
}
Kotlin #
class Solution {
fun mapWordWeights(words: Array<String>, weights: IntArray): String {
// StringBuilder is used to concatenate characters efficiently
val result = java.lang.StringBuilder()
// Loop over each word in the array
for (word in words) {
var wordWeight = 0
// Calculate the sum of weights for the characters in the word
for (c in word) {
wordWeight += weights[c - 'a']
}
// Modulo 26 for the mapping rule
val mappedVal = wordWeight % 26
// Calculate reverse alphabetical character starting from 'z'
val mappedChar = ('z' - mappedVal).toChar()
// Append the mapped character to the StringBuilder
result.append(mappedChar)
}
// Return the accumulated characters as a string
return result.toString()
}
}
Swift #
class Solution {
func mapWordWeights(_ words: [String], _ weights: [Int]) -> String {
// String to collect the mapped characters
var result = ""
// Process each word in the array
for word in words {
var wordWeight = 0
// Sum the weights of the characters in the current word
for char in word {
// Get the ASCII value of the character and subtract ASCII of 'a'
let charIndex = Int(char.asciiValue!) - Int(Character("a").asciiValue!)
wordWeight += weights[charIndex]
}
// Take the sum modulo 26
let mappedVal = wordWeight % 26
// Map the value using reverse alphabetical order
let mappedAscii = Int(Character("z").asciiValue!) - mappedVal
let mappedChar = Character(UnicodeScalar(mappedAscii)!)
// Append the mapped character
result.append(mappedChar)
}
return result
}
}
Other languages
Welcome to contribute code to GitHub repo -> 3838. Weighted Word Mapping. Thanks!Level Up Your Developer Identity
- 📄 Resume, Portfolio & Blog
- 🌐 Custom Domain Binding Supported
- ✨ Premium Subdomain:
https://yourname.coder.id