当前位置:网站首页>Leetcode-6108: decrypt messages

Leetcode-6108: decrypt messages

2022-07-05 06:09:00 Chrysanthemum headed bat

leetcode-6108: Decrypt the message

subject

Topic linking
Here is the string key and message , Represent an encryption key and an encrypted message respectively . Decrypt message The steps are as follows :

Use key in 26 The order of the first occurrence of English lowercase letters is used to replace the letters in the table The order .
Align the replacement table with the common English alphabet , Form a comparison table .
According to the comparison table Replace message Every letter in .
Space ' ' remain unchanged .
for example ,key = "happy boy"( The actual encryption key will contain every letter in the alphabet At least once ), Accordingly , You can get some comparison tables ('h' -> 'a''a' -> 'b’、'p' -> 'c''y' -> 'd''b' -> 'e''o' -> 'f').
Returns the decrypted message .

Example 1:
 Insert picture description here

 Input :key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
 Output :"this is a secret"
 explain : The comparison table is shown in the above figure .
 extract  "the quick brown fox jumps over the lazy dog"  The first occurrence of each letter in can get a replacement table .

Example 2:
 Insert picture description here

 Input :key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
 Output :"the five boxing wizards jump quickly"
 explain : The comparison table is shown in the above figure .
 extract  "eljuxhpwnyrdgtqkviszcfmabo"  The first occurrence of each letter in can get a replacement table .

 Insert picture description here

Problem solving

Method 1 : Hashtable

1. By hashing map, Record each letter in key The order in which they first appear , And assign corresponding letters .
2. Space directly skip , Replace letters

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;
    }
};
原网站

版权声明
本文为[Chrysanthemum headed bat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050546085368.html