当前位置:网站首页>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;
}
};
边栏推荐
- Sword finger offer 05 Replace spaces
- 2020ccpc Qinhuangdao J - Kingdom's power
- 每日一题-搜索二维矩阵ps二维数组的查找
- Daily question 1688 Number of matches in the competition
- Simply sort out the types of sockets
- Wazuh開源主機安全解决方案的簡介與使用體驗
- Daily question 1342 Number of operations to change the number to 0
- Binary search template
- [cloud native] record of feign custom configuration of microservices
- Simple knapsack, queue and stack with deque
猜你喜欢
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
AtCoder Grand Contest 013 E - Placing Squares
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Sword finger offer 09 Implementing queues with two stacks
Sword finger offer 35 Replication of complex linked list
读者写者模型
The connection and solution between the shortest Hamilton path and the traveling salesman problem
剑指 Offer 05. 替换空格
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Hang wait lock vs spin lock (where both are used)
随机推荐
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Software test -- 0 sequence
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
Introduction and experience of wazuh open source host security solution
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
Daily question 2013 Detect square
SAP method of modifying system table data
SSH password free login settings and use scripts to SSH login and execute instructions
Palindrome (csp-s-2021-palin) solution
Sword finger offer 09 Implementing queues with two stacks
The number of enclaves
26、 File system API (device sharing between applications; directory and file API)
Haut OJ 1401: praise energy
Transform optimization problems into decision-making problems
剑指 Offer 53 - I. 在排序数组中查找数字 I
Sword finger offer 58 - ii Rotate string left
Daily question - longest substring without repeated characters
Reader writer model
One question per day 1765 The highest point in the map
Fried chicken nuggets and fifa22