Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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:
"abcd" is 5 + 3 + 12 + 14 = 34. The result modulo 26 is 34 % 26 = 8, which maps to 'r'."def" is 14 + 1 + 2 = 17. The result modulo 26 is 17 % 26 = 17, which maps to 'i'."xyz" is 7 + 7 + 2 = 16. The result modulo 26 is 16 % 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.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
["abcd","def","xyz"] [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]
["a","b","c"] [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]
["abcd"] [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]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3838: Weighted Word Mapping
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();
}
}
// Accepted solution for LeetCode #3838: Weighted Word Mapping
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)
}
# Accepted solution for LeetCode #3838: Weighted Word Mapping
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)
// Accepted solution for LeetCode #3838: Weighted Word Mapping
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3838: Weighted Word Mapping
// 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();
// }
// }
// Accepted solution for LeetCode #3838: Weighted Word Mapping
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('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.