#2325
Decode the Message
newbie · 120 · lc easy +12 · verified · 85.8% accepted · 1,128 likes · top 97%
Description
You are given two strings key and message representing a cipher key and a coded message. To decode message:
- Scan key left to right and record the first occurrence of each lowercase letter in order.
- Map the 26 unique letters (in their first-appearance order) to 'a' through 'z'.
- Apply this substitution table to every character of message.
- Spaces are left unchanged.
- For example, with key = "happy boy", the first-seen letters are h, a, p, y, b, o, mapping to a, b, c, d, e, f respectively.
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".
Code
1
2
3