문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
문자열 key와 message가 주어지고, 암호 키와 비밀 메시지를 나타낸다. message를 해독하는 단계는 다음과 같다.
해독된 메시지를 반환해라.
#1
Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
Output: "this is a secret"
Explanation: 위 다이어그램은 치환표를 보여준다.
"the quick brown fox jumps over the lazy dog"에서 처음 나타난 글자들로 얻을 수 있다.
#2
Input: key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
Output: "the five boxing wizards jump quickly"
class Solution {
public String decodeMessage(String key, String message) {
int a = 97;
Map<Character, Character> dictionary = new HashMap<>(key.length());
for(int i = 0; i < key.length(); i++){
char k = key.charAt(i);
if(k != ' '){
if(!dictionary.containsKey(k)){
dictionary.put(k, (char) a++);
}
}
}
StringBuilder sb = new StringBuilder(message.length());
for(int i = 0; i < message.length(); i++){
char decoded = ' ';
if(message.charAt(i) != ' '){
decoded = dictionary.get(message.charAt(i));
sb.append(decoded);
}else{
sb.append(decoded);
}
}
return sb.toString();
}
}