You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows:
- Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
- Align the substitution table with the regular English alphabet.
- Each letter in message is then substituted using the table.
- Spaces ' ' are transformed to themselves.
- For example, given key = "happy boy" (actual key would have at least one instance of each letter in the alphabet), we have the partial substitution table of ('h' -> 'a', 'a' -> 'b', 'p' -> 'c', 'y' -> 'd', 'b' -> 'e', 'o' -> 'f').
Return the decoded message.
Example 1:

Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
Output: "this is a secret"
Explanation: The diagram above shows the substitution table.
It is obtained by taking the first appearance of each letter in "the quick brown fox jumps over the lazy dog".
Example 2:

Input: key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
Output: "the five boxing wizards jump quickly"
Explanation: The diagram above shows the substitution table.
It is obtained by taking the first appearance of each letter in "eljuxhpwnyrdgtqkviszcfmabo".
출처 : https://leetcode.com/problems/decode-the-message/description/
풀이
function decodeMessage(key: string, message: string) {
// 알파벳 문자를 담은 배열
let alfabet = ['a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' ,'l' ,'m' ,'n' , 'o' ,'p' ,'q' , 'r' ,'s' ,'t', 'u' , 'v' ,'w' , 'x' ,'y' , 'z' ];
// key 문자열에서 공백을 제거하고, 중복된 문자를 제거한 후 배열로 변환
let keyArray = Array.from(new Set(key.split(" ").join("").split("")));
// message 문자열을 각 문자로 분할하여 배열로 변환
let messageArray = message.split('');
// messageArray의 각 문자를 findKey 함수로 변환하고, 이를 다시 문자열로 결합하여 반환
return messageArray.map((value)=>{
return findKey(value);
}).join("");
// keyArray에서 문자의 위치를 찾아 해당 위치의 알파벳 문자를 반환하는 함수
function findKey (str: string){
for(let i= 0 ; i< keyArray.length ; i++){
if(str == keyArray[i]){
return alfabet[i];
}
}
// keyArray에 문자가 없으면 공백을 반환
return " ";
}
}
let keyArray = Array.from(new Set(key.split(" ").join("").split("")));
// 1. 공백을 기준으로 key를 분할
let splitKey = key.split(" "); // ["the", "quick", "brown", "fox"]
// 2. 분할된 문자열을 다시 결합 (공백 없이)
let joinedKey = splitKey.join(""); // "thequickbrownfox"
// 3. 결합된 문자열을 문자 단위로 분할
let charArray = joinedKey.split(""); // ["t", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o", "w", "n", "f", "o", "x"]
// 4. 중복된 문자를 제거하여 Set 객체 생성
let uniqueCharSet = new Set(charArray); // {"t", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o", "w", "n", "f", "x"}
// 5. Set 객체를 다시 배열로 변환
let keyArray = Array.from(uniqueCharSet); // ["t", "h", "e", "q", "u", "i", "c", "k", "b", "r", "o", "w", "n", "f", "x"]
'LeetCode' 카테고리의 다른 글
| 868. Binary Gap / TypeScript (0) | 2024.06.10 |
|---|---|
| 3033. Modify the Matrix / TypeScript (1) | 2024.06.09 |
| 2287. Rearrange Characters to Make Target String / TypeScript (0) | 2024.06.04 |
| 1407. Top Travellers (0) | 2024.06.03 |
| 1672. Richest Customer Wealth / TypeScript (1) | 2024.06.02 |