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' -&gt; 'a', 'a' -&gt; 'b', 'p' -&gt; 'c', 'y' -&gt; 'd', 'b' -&gt; 'e', 'o' -&gt; 'f').


Return the decoded message.

&nbsp;
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".


&nbsp;
Constraints:


	26 &lt;= key.length &lt;= 2000
	key consists of lowercase English letters and ' '.
	key contains every letter in the English alphabet ('a' to 'z') at least once.
	1 &lt;= message.length &lt;= 2000
	message consists of lowercase English letters and ' '.


class Solution:
    def decodeMessage(self, key: str, message: str) -> str:
        alpha = ['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']
        i=0
        d={}
        for j in key:
            if j!=" " and j not in d:
                d[j]=alpha[i]
                i+=1
            if len(d)==26:
                break
        res=""
        d[" "]=" "
        for i in message:
            res+=d[i]
        return res
class Solution {
    public String decodeMessage(String key, String message) {
        StringBuilder ans = new StringBuilder();//Using String Builder to append the string
        key = key.replaceAll(" ", "");
        //Removing the spaces
        HashMap<Character,Character> letters = new HashMap<>();
        //Mapping the key into a hashmap.
        char original = 'a';
        for (int i = 0; i < key.length() ; i++) {
            if (!letters.containsKey(key.charAt(i))){
                letters.put(key.charAt(i),original++);
            }
        }
        //After the first pass all the letters of the key will be mapped with their respective original letters.
        for (int i = 0; i < message.length(); i++) {
            if (letters.containsKey(message.charAt(i))){
                //Now replacing the letters of the message with appropriate letter according to the key
                ans.append(letters.get(message.charAt(i)));
            }else{
                ans.append(message.charAt(i));
                //This is for characters other than the letters in the key example a space " "
                //They will not be replaced by any letters hence original letter is appended into the StringBuilder
            }
        }
        return ans.toString();
    }
}
class Solution {
public:
    string decodeMessage(string key, string message) {
        vector <char> alpha {'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'};
        int i=0;

        map<char,char> d;

        for(int j=0;j<26;j++){
            d[alpha[j]]='0';
        }

        for(int j=0; j<key.size(); j++){

            if(key[j]!=' ' && d[key[j]]=='0'){
                d[key[j]]=alpha[i];
                i++;
            }
            if (i==26) break;
        }
        d[' '] = ' ';

        string res="";
        for(int i=0; i<message.size(); i++){
            res += d[message[i]];
        }
        return res;
    }
};
var decodeMessage = function(key, message) {
  let result = ''
  key = Array.from(new Set(key.split(' ').join('')))
  const hash = new Map()
  const alpha = 'abcdefghijklmnopqrstuvwxyz'
  
  for (let i = 0; i < alpha.length; i++) {
    hash.set(key[i], alpha[i])
  }

  for (let chr of message) {
    result += hash.get(chr) || ' '
  }
    
  return result
};
Decode the Message