給你字元串 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” 中每個字母的首次出現可以得到替換表。
提示:
26 <= key.length <= 2000
key 由小寫英文字母及 ’ ’ 組成
key 包含英文字母表中每個字元(‘a’ 到 ‘z’)至少一次
1 <= message.length <= 2000
message 由小寫英文字母和 ’ ’ 組成
public String decodeMessage(String key, String message) {
Map<Character, Character> map = new HashMap<>();
map.put(' ',' ');
StringBuilder res = new StringBuilder(message.length());
char index='a';
for (int i = 0; i < key.length(); i++) {
if (!map.containsKey(key.charAt(i))){
map.put(key.charAt(i),index++);
}
}
for (int i = 0; i < message.length(); i++) {
res.append(map.get(message.charAt(i)));
}
return res.toString();
}
func decodeMessage(key string, message string) string {
mapX:=make(map[int32]int32,27)
mapX[' ']=' '
res :=""
index:='a'
for _, v := range key {
if _,ok:=mapX[v];!ok {
mapX[v]=index
index++
}
}
for _, v := range message {
res+=string(mapX[v])
}
return res
}
func decodeMessage(key string, message string) string {
mapX:=make(map[int32]int32,27)
mapX[' ']=' '
res:=make([]int32, len(message))
index:='a'
for _, v := range key {
if _,ok:=mapX[v];!ok {
mapX[v]=index
index++
}
}
for k, v := range message {
res[k]=mapX[v]
}
return string(res)
}