给你字符串 key 和 message ,分别表示一个加密密钥和一段加密消息。解密 message 的步骤如下:
key 中 26 个英文小写字母第一次出现的顺序作为替换表中的字母 顺序 。message 中的每个字母。' ' 保持不变。key = "happy boy"(实际的加密密钥会包含字母表中每个字母 至少一次),据此,可以得到部分对照表('h' -> 'a'、'a' -> 'b'、'p' -> 'c'、'y' -> 'd'、'b' -> 'e'、'o' -> 'f')。返回解密后的消息。

输入:
key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
输出:
"this is a secret"
解释:
对照表如上图所示。
提取 "the quick brown fox jumps over the lazy dog" 中每个字母的首次出现可以得到替换表。

输入:
key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
输出:
"the five boxing wizards jump quickly"
解释:
对照表如上图所示。
提取 "eljuxhpwnyrdgtqkviszcfmabo" 中每个字母的首次出现可以得到替换表。
26 <= key.length <= 2000key 由小写英文字母及 ' ' 组成key 包含英文字母表中每个字符('a' 到 'z')至少一次1 <= message.length <= 2000message 由小写英文字母和 ' ' 组成impl Solution {
pub fn decode_message(key: String, mut message: String) -> String {
// 生成映射表
let mut tab = vec![0u8; 26];
let mut k = 97u8;
key.as_bytes().iter().for_each(|&b| {
if b != ' ' as u8 && tab[(b - 97) as usize] == 0 {
tab[(b - 97) as usize] = k;
k += 1;
}
});
// 解密
unsafe {
message.as_bytes_mut().iter_mut().for_each(|b| {
if *b != ' ' as u8 {
*b = tab[(*b - 97) as usize];
}
});
}
message
}
}
func decodeMessage(key string, message string) string {
// 生成对照表
tab := [26]byte{}
k := byte('a')
for _, b := range key {
if b != ' ' && tab[b-'a'] == 0 {
tab[b-'a'] = k
k++
}
}
// 解密消息
bs := []byte(message)
for i, b := range bs {
if b != ' ' {
bs[i] = tab[b-'a']
}
}
return string(bs)
}
function decodeMessage(key: string, message: string): string {
// 生成对照表
const tab = { ' ': ' ' };
let k = 97;
for (const c of key) {
if (!tab[c]) {
tab[c] = String.fromCharCode(k);
++k;
}
}
// 解密消息
return message.split('').map(c => tab[c]).join('');
};
class Solution:
def decodeMessage(self, key: str, message: str) -> str:
# 生成对照表
tab = {' ': ' '}
i = 0
for c in key:
if c not in tab:
tab[c] = ascii_lowercase[i]
i += 1
# 解密消息
return ''.join(tab[c] for c in message)
char * decodeMessage(char * key, char * message){
// 生成对照表
char tab[26] = {};
char k = 'a';
while (*key) {
if (*key != ' ' && tab[(*key) - 'a'] == 0) {
tab[(*key) - 'a'] = k++;
}
++key;
}
// 解密消息
char *t = message;
while (*t) {
if (*t != ' ') {
*t = tab[(*t) - 'a'];
}
++t;
}
return message;
}
class Solution {
public:
string decodeMessage(string key, string message) {
// 生成对照表
char tab[26] = {};
char k = 'a';
for (auto c: key) {
if (c != ' ' && tab[c - 'a'] == 0) {
tab[c - 'a'] = k++;
}
}
// 解密消息
for (int i = 0; i < message.length(); ++i) {
if (message[i] != ' ') {
message[i] = tab[message[i] - 'a'];
}
}
return message;
}
};
class Solution {
public String decodeMessage(String key, String message) {
// 生成对照表
char[] tab = new char[26];
char k = 'a';
for (char c : key.toCharArray()) {
if (c != ' '
&& tab[c - 'a'] == 0) {
tab[c - 'a'] = k++;
}
}
// 解密消息
char[] cs = message.toCharArray();
for (int i = 0; i < cs.length; ++i) {
char c = cs[i];
if (c != ' ') {
cs[i] = tab[c - 'a'];
}
}
return String.valueOf(cs);
}
}
非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~