问题描述 链接到标题

2325.解密消息

解题思路 链接到标题

利用数组作为哈希表,记录每个字母是第几个出现的

代码 链接到标题

class Solution {
public:
    string decodeMessage(string key, string message) {
        int cnt = 0;
        vector<int> arr(26, -1); // 为-1表示这个字母从未出现过,0,1,2等表示这个字母对应的解密字母
        for (auto &c : key) {
            if (c != ' ') {
                if (arr[c - 'a'] == -1) {
                    arr[c - 'a'] = cnt++;
                    // cnt++;
                }
            }
        }
        string res;
        for (auto &c : message) {
            if (c != ' ')
                res.push_back('a' + arr[c - 'a']);
            else
                res.push_back(c);
        }
        return res;
    }
};