Weighted Word Mapping solution and explanation
9 more videos on this problem
3838. Weighted Word Mapping | Leetcode Daily - PythonLeetcode Daily · 3:51 · 437 views
Weighted Word Mapping | LeetCode 3838 | Java Code | Developer CoderDeveloper Coder · 9:46 · 430 views
Daily Leetcode #2265: Jun 13, 2026 - 3838. Weighted Word MappingProgramming Live with Larry · 5:29 · 312 views
LeetCode 3838 : Weighted Word Mapping | O(N) String & Modulo Math | @DCodeThoughtDCode Thought · 6:47 · 285 views
Weighted Word Mapping | LeetCode 3838Streak To Scale · 2:30 · 236 views
Weighted Word Mapping | LeetCode 3838 - Pythonimpoldev · 6:23 · 183 views
3838. Weighted Word Mapping (Leetcode Easy)Programming Live with Larry · 3:46 · 165 views
LeetCode Problem 3838 | Weighted Word MappingRepovive TV · 3:27 · 129 views
3838. Weighted Word Mapping | POTD 13 June 2026 | C++ | LeetcodeTechie On Track · 5:35 · 114 views
Weighted Word Mapping | Leetcode 3838 | Explanation With Code | Javacodewithsitaram · 23:34 · 106 views
The problem
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.
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.
Approach overview
Problem Overview: You are given words and a predefined weight mapping for characters. Each word’s value is determined by summing the weights of its characters. The task is to simulate this mapping and compute the resulting weighted values for the given words or validate the mapping behavior.
Approach 1: Direct Simulation (O(n * m) time, O(1) space)
The straightforward way is to simulate the mapping exactly as defined. Store the character weights in an array of size 26 so each lookup takes constant time. Iterate through every word, and for each character, fetch its weight and add it to a running sum. If the problem requires comparing or storing results, maintain a simple array or hash map of computed weights. The algorithm processes n words with average length m, giving O(n * m) time complexity and O(1) auxiliary space since the weight table size is fixed.
Approach 2: Precomputed Character Lookup Table (O(n * m) time, O(1) space)
If the weight mapping is given in another structure (such as pairs or a dictionary), first convert it into a fixed-size lookup table indexed by character. This removes repeated hash lookups during processing. After building the table, iterate through each word and accumulate weights using simple array indexing. This approach still runs in O(n * m) time but improves constant factors and keeps memory usage minimal. The logic is essentially a lightweight simulation combined with basic string traversal.
Approach 3: Aggregation with Hash Map (O(n * m) time, O(k) space)
When the task requires grouping or tracking words by their computed weight, store results in a hash map where the key is the computed weight and the value contains the corresponding words or counts. Each word is processed once, and its weight is calculated using the same character iteration strategy. The extra map introduces O(k) space where k is the number of distinct weight values. This approach appears in problems that combine array processing with string evaluation.
Recommended for interviews: The direct simulation with a constant-size weight lookup table is the expected solution. It is simple, efficient, and easy to reason about. Interviewers mainly check whether you model the mapping correctly and avoid unnecessary overhead. Explaining the brute-force simulation first shows understanding of the problem, while implementing the optimized lookup table demonstrates clean engineering thinking.
Solution
We iterate through each word w in words, calculate its weight s, which is the sum of the weights of all characters in the word. Then we calculate s modulo 26, map the result to a lowercase English letter, and finally concatenate all the mapped characters and return.
The time complexity is O(L), where L is the sum of the lengths of all words in words. The space complexity is O(W), where W is the length of words.
class Solution:
def mapWordWeights(self, words: List[str], weights: List[int]) -> str:
ans = []
for w in words:
s = sum(weights[ord(c) - ord('a')] for c in w)
ans.append(ascii_lowercase[25 - s % 26])
return ''.join(ans)class Solution {
public String mapWordWeights(String[] words, int[] weights) {
var ans = new StringBuilder();
for (var w : words) {
int s = 0;
for (char c : w.toCharArray()) {
s = (s + weights[c - 'a']) % 26;
}
ans.append((char) ('a' + (25 - s)));
}
return ans.toString();
}
}class Solution {
public:
string mapWordWeights(vector<string>& words, vector<int>& weights) {
string ans;
for (const string& w : words) {
int s = 0;
for (char c : w) {
s = (s + weights[c - 'a']) % 26;
}
ans.push_back(char('a' + (25 - s)));
}
return ans;
}
};function mapWordWeights(words: string[], weights: number[]): string {
const ans: string[] = [];
for (const w of words) {
let s = 0;
for (const c of w) {
s = (s + weights[c.charCodeAt(0) - 97]) % 26;
}
ans.push(String.fromCharCode(97 + (25 - s)));
}
return ans.join('');
}func mapWordWeights(words []string, weights []int) string {
ans := make([]byte, 0, len(words))
for _, w := range words {
s := 0
for i := 0; i < len(w); i++ {
s = (s + weights[int(w[i]-'a')]) % 26
}
ans = append(ans, byte('a'+(25-s)))
}
return string(ans)
}Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation | O(n * m) | O(1) | Best for straightforward evaluation of word weights using a fixed character mapping |
| Precomputed Character Lookup Table | O(n * m) | O(1) | When the weight mapping is provided in a non-indexed format and needs faster repeated lookups |
| Aggregation with Hash Map | O(n * m) | O(k) | When grouping or counting words by their computed weight values |
Frequently asked questions
Is Weighted Word Mapping easy or hard?
Weighted Word Mapping is considered an easy problem because it mainly involves string traversal and simple simulation. The challenge is minimal once the character-to-weight lookup structure is prepared.
Weighted Word Mapping Python/Java solution
In Python or Java, the solution typically creates an array or dictionary storing character weights, then iterates through each word and accumulates the weight of its characters. The implementation is short and runs in O(n * m) time with constant extra space.
How to solve Weighted Word Mapping in O(n)?
Strict O(n) is possible only if the total number of processed characters is proportional to n. In practice the complexity is O(n * m) because every character in each word must be inspected to compute the weight. Using a direct lookup table ensures the constant factor remains very small.
What is the best approach for Weighted Word Mapping?
The best approach is a direct simulation using a constant-size character weight lookup table. Iterate through each word and sum the weights of its characters using array indexing. This method runs in O(n * m) time, where n is the number of words and m is the average word length, while using O(1) extra space.
Is Weighted Word Mapping asked at Google/Amazon/Meta?
Problems involving weighted character mappings and string simulation appear in interviews at companies like Amazon and Google. They test a candidate’s ability to model mappings, iterate through strings efficiently, and implement clean simulation logic.
What data structure is used in Weighted Word Mapping?
The most common data structure is a fixed-size array of length 26 that stores the weight for each lowercase character. Some variations may also use a hash map to store or group computed word weights.
What is the time complexity of Weighted Word Mapping?
The typical solution runs in O(n * m) time. Each of the n words is processed character by character, and each lookup in the weight table takes constant time. Space complexity is usually O(1) because the character weight mapping is fixed in size.
