Description Link to heading

2325.decode-the-message

Solution Link to heading

We use arrya as a hash table, to keep track of the order in which the letter appear.

Code Link to heading

class Solution {
public:
    string decodeMessage(string key, string message) {
        int cnt = 0;
        vector<int> arr(26, -1); // -1 means the letter never appears, other value means the order in which the letter appears
        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;
    }
};