当前位置:网站首页>leetcode-6108:解密消息
leetcode-6108:解密消息
2022-07-05 05:46:00 【菊头蝙蝠】
题目
题目连接
给你字符串 key 和 message ,分别表示一个加密密钥和一段加密消息。解密 message 的步骤如下:
使用 key 中 26 个英文小写字母第一次出现的顺序作为替换表中的字母 顺序 。
将替换表与普通英文字母表对齐,形成对照表。
按照对照表 替换 message 中的每个字母。
空格 ' ' 保持不变。
例如,key = "happy boy"(实际的加密密钥会包含字母表中每个字母 至少一次),据此,可以得到部分对照表('h' -> 'a'、'a' -> 'b’、'p' -> 'c'、'y' -> 'd'、'b' -> 'e'、'o' -> 'f')。
返回解密后的消息。
示例 1:
输入: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" 中每个字母的首次出现可以得到替换表。
示例 2:
输入:key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
输出:"the five boxing wizards jump quickly"
解释:对照表如上图所示。
提取 "eljuxhpwnyrdgtqkviszcfmabo" 中每个字母的首次出现可以得到替换表。

解题
方法一:哈希表
1.通过哈希map,记录每个字母在key中第一次出现的顺序,并赋于相应的字母。
2.空格直接跳过,替换字母
class Solution {
public:
string decodeMessage(string key, string message) {
unordered_map<char,char> map;
char c='a';
for(int i=0;i<key.size();i++){
if(key[i]==' ') continue;
if(!map.count(key[i])){
map[key[i]]=c;
c++;
}
}
for(int i=0;i<message.size();i++){
if(message[i]==' ') continue;
char c=message[i];
message[i]=map[c];
}
return message;
}
};
边栏推荐
- Using HashMap to realize simple cache
- AtCoder Grand Contest 013 E - Placing Squares
- sync. Interpretation of mutex source code
- 卷积神经网络——卷积层
- F - Two Exam(AtCoder Beginner Contest 238)
- SSH password free login settings and use scripts to SSH login and execute instructions
- Graduation project of game mall
- 二十六、文件系统API(设备在应用间的共享;目录和文件API)
- 读者写者模型
- Chapter 6 data flow modeling - after class exercises
猜你喜欢

Dynamic planning solution ideas and summary (30000 words)

SAP method of modifying system table data

Acwing 4300. Two operations

Sword finger offer 35 Replication of complex linked list

【Jailhouse 文章】Jailhouse Hypervisor

Reader writer model

剑指 Offer 09. 用两个栈实现队列

Sword finger offer 05 Replace spaces

Hang wait lock vs spin lock (where both are used)

Wazuh開源主機安全解决方案的簡介與使用體驗
随机推荐
个人开发的渗透测试工具Satania v1.2更新
SAP method of modifying system table data
[practical skills] technical management of managers with non-technical background
CF1634E Fair Share
Common optimization methods
YOLOv5-Shufflenetv2
Control unit
Sword finger offer 04 Search in two-dimensional array
[cloud native] record of feign custom configuration of microservices
Hang wait lock vs spin lock (where both are used)
Cluster script of data warehouse project
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
Sword finger offer 53 - I. find the number I in the sorted array
【Jailhouse 文章】Jailhouse Hypervisor
Sword finger offer 53 - ii Missing numbers from 0 to n-1
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
Dynamic planning solution ideas and summary (30000 words)
QT判断界面当前点击的按钮和当前鼠标坐标
Brief introduction to tcp/ip protocol stack
A misunderstanding about the console window