Easy

题目描述

给定一个字符串数组 words,其中每个字符串表示一个包含小写英文字母的单词。

还给定一个长度为 26 的整数数组 weights,其中 weights[i] 表示第 i 个小写英文字母的权重。

单词的权重定义为其字符权重的总和。

对于每个单词,取其权重对 26 取模的结果,并使用逆字母顺序将结果映射到小写英文字母(0 -> ‘z’, 1 -> ‘y’, …, 25 -> ‘a’)。

返回由所有单词按顺序映射的字符连接形成的字符串。

示例 1:

输入: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]
输出:"rij"
解释:
- "abcd" 的权重是 5 + 3 + 12 + 14 = 34。34 % 26 = 8,映射到 'r'。
- "def" 的权重是 14 + 1 + 2 = 17。17 % 26 = 17,映射到 'i'。
- "xyz" 的权重是 7 + 7 + 2 = 16。16 % 26 = 16,映射到 'j'。

示例 2:

输入: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]
输出:"yyy"
解释:每个单词的权重都是 1。1 % 26 = 1,映射到 'y'。

示例 3:

输入: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]
输出:"g"
解释:"abcd" 的权重是 7 + 5 + 3 + 4 = 19。19 % 26 = 19,映射到 'g'。

约束条件:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 10
  • weights.length == 26
  • 1 <= weights[i] <= 100
  • words[i] 由小写英文字母组成

解题思路

这是一道模拟题,需要按照题目描述的步骤依次处理。

解题思路:

  1. 计算单词权重:对于每个单词,遍历其所有字符,通过 weights[c - 'a'] 获取字符权重并累加。

  2. 取模运算:将单词的总权重对 26 取模,得到一个 0-25 范围内的数值。

  3. 逆序映射:关键在于理解"逆字母顺序"的含义。正常字母顺序是 a=0, b=1, …, z=25,而逆序是 z=0, y=1, …, a=25。因此映射公式为 char = 'z' - value

  4. 拼接结果:将所有映射后的字符按顺序连接形成最终字符串。

这道题的核心是理解映射规则,特别是"逆字母顺序"的概念。算法本身非常直观,就是按步骤模拟即可。

时间复杂度分析:需要遍历所有单词的所有字符,总字符数最多为 100×10 = 1000,所以时间复杂度为 O(n×m),其中 n 是单词数量,m 是单词平均长度。

代码实现

class Solution {
public:
    string mapWordWeights(vector<string>& words, vector<int>& weights) {
        string result = "";
        
        for (const string& word : words) {
            int totalWeight = 0;
            
            // 计算单词的总权重
            for (char c : word) {
                totalWeight += weights[c - 'a'];
            }
            
            // 取模并映射到字符
            int mappedValue = totalWeight % 26;
            char mappedChar = 'z' - mappedValue;
            result += mappedChar;
        }
        
        return result;
    }
};
class Solution:
    def mapWordWeights(self, words: List[str], weights: List[int]) -> str:
        result = []
        
        for word in words:
            total_weight = 0
            
            # 计算单词的总权重
            for c in word:
                total_weight += weights[ord(c) - ord('a')]
            
            # 取模并映射到字符
            mapped_value = total_weight % 26
            mapped_char = chr(ord('z') - mapped_value)
            result.append(mapped_char)
        
        return ''.join(result)
public class Solution {
    public string MapWordWeights(string[] words, int[] weights) {
        StringBuilder result = new StringBuilder();
        
        foreach (string word in words) {
            int totalWeight = 0;
            
            // 计算单词的总权重
            foreach (char c in word) {
                totalWeight += weights[c - 'a'];
            }
            
            // 取模并映射到字符
            int mappedValue = totalWeight % 26;
            char mappedChar = (char)('z' - mappedValue);
            result.Append(mappedChar);
        }
        
        return result.ToString();
    }
}
var mapWordWeights = function(words, weights) {
    let result = "";
    
    for (const word of words) {
        let totalWeight = 0;
        
        // 计算单词的总权重
        for (const c of word) {
            totalWeight += weights[c.charCodeAt(0) - 'a'.charCodeAt(0)];
        }
        
        // 取模并映射到字符
        const mappedValue = totalWeight % 26;
        const mappedChar = String.fromCharCode('z'.charCodeAt(0) - mappedValue);
        result += mappedChar;
    }
    
    return result;
};

复杂度分析

复杂度类型复杂度说明
时间复杂度O(n×m)n 为单词数量,m 为单词平均长度,需要遍历所有字符
空间复杂度O(1)除了结果字符串外,只使用常数额外空间