Weighted Word Mapping

Step-by-Step

Easy • Simulation

Given an array of strings words and an integer array weights of length 26. The weight of a word is 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 the concatenated string.

init
process_word
process_char
map
done

Words Array

words[0]
a
b
c
d
words[1]
d
e
f
words[2]
x
y
z

Weights Array (a-z)

a5
b3
c12
d14
e1
f2
g3
h2
i10
j6
k6
l9
m7
n8
o7
p10
q8
r9
s6
t9
u9
v8
w3
x7
y7
z2

Mapping & Result

Current Sum-
Mod 26-
Mapped Char
?
Result String
Empty

Step log

Initialize: We will process each word, calculate its weight, and map it to a character.
naive.py
1def solve(words, weights):
2 res = []
3 for w in words:
4 weight_sum = 0
5 for c in w:
6 weight_sum += weights[ord(c) - ord('a')]
7 mapped_val = weight_sum % 26
8 res.append(chr(ord('z') - mapped_val))
9 return ''.join(res)

Test Cases

Basic case

Input Configuration

Ensure exactly 26 weights are provided.

Animation Controls

Speed700ms
1/18

Complexity Analysis

Time

O(N)

Space

O(N)

Where N is the total number of characters across all words. We iterate through each character once.