Decode the Message LeetCode solution in C++

1
class Solution {
2
public:
3
string decodeMessage(string key, string message) {
4
vector<char> alpha{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
5
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
6
int i = 0;
7

8
map<char, char> d;
9

10
for (int j = 0; j < 26; j++) {
11
d[alpha[j]] = '0';
12
}
13

14
for (int j = 0; j < key.size(); j++) {
15
if (key[j] != ' ' && d[key[j]] == '0') {
16
d[key[j]] = alpha[i];
17
i++;
18
}
19
if (i == 26) break;
20
}
21
d[' '] = ' ';
22

23
string res = "";
24
for (int i = 0; i < message.size(); i++) {
25
res += d[message[i]];
26
}
27
return res;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0