Skip to main content
Back to problems
#3838
Easy Algorithms

Weighted word mapping

Array String Simulation
85.3% acceptance
Mar 15, 2026
35
2
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 ith 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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn map_word_weights(words: Vec<String>, weights: Vec<i32>) -> String {
    words.iter().map(|word| {
      let w: i32 = word.bytes().map(|b| weights[(b - b'a') as usize]).sum();
      let r = (w % 26) as u8;
      (b'z' - r) as char
    }).collect()
  }
}