Skip to content

Latest commit

 

History

History

2325.Decode the Message

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

English Version

题目描述

给你字符串 keymessage ,分别表示一个加密密钥和一段加密消息。解密 message 的步骤如下:

  1. 使用 key 中 26 个英文小写字母第一次出现的顺序作为替换表中的字母 顺序
  2. 将替换表与普通英文字母表对齐,形成对照表。
  3. 按照对照表 替换 message 中的每个字母。
  4. 空格 ' ' 保持不变。
  • 例如,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" 中每个字母的首次出现可以得到替换表。

 

提示:

  • 26 <= key.length <= 2000
  • key 由小写英文字母及 ' ' 组成
  • key 包含英文字母表中每个字符('a''z'至少一次
  • 1 <= message.length <= 2000
  • message 由小写英文字母和 ' ' 组成

解法

Python3

class Solution:
    def decodeMessage(self, key: str, message: str) -> str:
        d = {" ": " "}
        i = 0
        for c in key:
            if c in d:
                continue
            d[c] = ascii_lowercase[i]
            i += 1
        return "".join(d[c] for c in message)

Java

class Solution {
    public String decodeMessage(String key, String message) {
        Map<Character, Character> d = new HashMap<>();
        String lowcase = "abcdefghijklmnopqrstuvwxyz";
        d.put(' ', ' ');
        int i = 0;
        for (char c : key.toCharArray()) {
            if (d.containsKey(c)) {
                continue;
            }
            d.put(c, lowcase.charAt(i++));
        }
        StringBuilder ans = new StringBuilder();
        for (char c : message.toCharArray()) {
            ans.append(d.get(c));
        }
        return ans.toString();
    }
}

C++

class Solution {
public:
    string decodeMessage(string key, string message) {
        unordered_map<char, char> d;
        d[' '] = ' ';
        int i = 0;
        string lowcase = "abcdefghijklmnopqrstuvwxyz";
        for (char c : key) {
            if (d.count(c)) continue;
            d[c] = lowcase[i]++;
        }
        string ans;
        for (char c : message) ans.push_back(d[c]);
        return ans;
    }
};

Go

func decodeMessage(key string, message string) string {
	d := map[rune]byte{}
	d[' '] = ' '
	i := 0
	lowcase := "abcdefghijklmnopqrstuvwxyz"
	for _, c := range key {
		if _, ok := d[c]; ok {
			continue
		}
		d[c] = lowcase[i]
		i++
	}
	var ans []byte
	for _, c := range message {
		ans = append(ans, d[c])
	}
	return string(ans)
}

TypeScript

function decodeMessage(key: string, message: string): string {
    let decodeMap = new Map();
    const m = key.length,
        n = 26;
    for (let i = 0, j = 0; i < m; i++) {
        let char = key.charAt(i);
        if (char != ' ' && !decodeMap.has(char)) {
            decodeMap.set(char, String.fromCharCode(j + 97));
            j++;
        }
    }
    let ans = [];
    for (let char of message) {
        ans.push(char == ' ' ? ' ' : decodeMap.get(char));
    }
    return ans.join('');
}

...