2 minute read

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”.

/**
 * @param {string} key
 * @param {string} message
 * @return {string}
 */
var decodeMessage = function (key, message) {
  // Create a map to store the mapping with key and ascii code.
  let map = new Map();
  // Get rid of all spaces in the key value
  let pureKey = key.replaceAll(" ", "");
  // Initialize a variable with the value 97 which is for lowercase letter 'a'
  let asciiCode = 97;

  // Iterate through each character in the string key.
  for (let i = 0; i < pureKey.length; i++) {
    // If a word isn't exist in the map,
    if (!map.has(pureKey[i])) {
      // the word is assigned in the map with asciiCode. And then, increment asciiCode.
      map.set(pureKey[i], asciiCode++);
    }
  }
  // Initialize a variable to store converted message.
  let result = "";
  // Loop through the message string from its parameter.
  for (let i = 0; i < message.length; i++) {
    // Convert ascii code back to a character
    result += String.fromCharCode(map.get(message[i]));
  }
  // return result without null value.
  return result.replaceAll("\u0000", " ");
};

A Map named map is created to store the mapping between characters in the key and their corresponding ASCII codes.

The key is cleaned of spaces by using key.replaceAll(" ", ""), resulting in the pureKey, which is used to define the order for assigning ASCII codes.

An asciiCode variable is initialized with the value 97, which corresponds to the ASCII code for the lowercase letter ‘a’.

A for loop is used to iterate through each character in the pureKey. For each character, it checks if it is already in the map using map.has(pureKey[i]). If it’s not in the map, it associates the character with the current asciiCode value and then increments asciiCode for the next character.

After creating the mapping in map, a result variable is initialized as an empty string.

Another for loop is used to iterate through each character in the message. For each character, it looks up its corresponding ASCII code in the map using map.get(message[i]). It then converts the ASCII code back to a character using String.fromCharCode() and appends it to the result string.

Finally, any occurrences of the null character (\u0000) in the result are replaced with a space character using result.replaceAll("\u0000", " ").

Leave a comment