Decode the Message - Problem

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:

  1. Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
  2. Align the substitution table with the regular English alphabet.
  3. Each letter in message is then substituted using the table.
  4. 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.

Input & Output

Example 1 — Basic Substitution
$ Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
Output: this is a secret
💡 Note: Key contains all 26 letters. First unique chars: t→a, h→b, e→c, q→d, u→e, i→f, c→g, k→h, etc. Decoding 'v'→'s', 'k'→'h', 'b'→'i', 's'→'t' gives 'this is a secret'
Example 2 — With Spaces
$ Input: key = "happy boy", message = "h p"
Output: a c
💡 Note: Key gives mapping h→a, a→b, p→c, y→d, b→e, o→f. Message 'h p' becomes 'a c' (spaces preserved)
Example 3 — Single Character
$ Input: key = "abcdefghijklmnopqrstuvwxyz", message = "z"
Output: z
💡 Note: Key is regular alphabet, so z→z (26th letter maps to 26th position)

Constraints

  • 1 ≤ key.length ≤ 105
  • 1 ≤ message.length ≤ 104
  • key contains every letter of the English alphabet at least once
  • key and message consist of lowercase English letters and spaces

Visualization

Tap to expand
Decode the Message - Hash Map Substitution INPUT Key String: "the quick brown fox jumps over the lazy dog" (contains all 26 letters) First Appearances: t h e q u i c k b r o w n,f,x,j,m,p,s,v,l,a,z,y,d,g Message to Decode: "vkbs bs t suepuv" Input Values key: 26 unique letters message: "vkbs bs t suepuv" Length: 16 characters Spaces: 3 ALGORITHM STEPS 1 Build Substitution Map Extract first occurrence of each letter 2 Create Mapping Table Map key order to a,b,c,d... Substitution Table Cipher: t h e q u i c k b r o w n... Plain: a b c d e f g h i j k l m... v-->s, k-->h, b-->i, s-->t 3 Substitute Each Char Look up each letter in map 4 Preserve Spaces Spaces remain unchanged Hash Map: O(1) Lookup map['v'] = 's' map['k'] = 'h' FINAL RESULT Character by Character: In: v k b s b s t s u e p u v | | | | | | | | | | | | | Out: t h i s i s a s e c r e t Decoded Message: "this is a secret" Output Value "this is a secret" OK - Decoded! 16 chars processed Key Insight: The key contains all 26 letters. The first appearance of each letter determines its mapping position. Using a Hash Map provides O(1) lookup for each character, making total time complexity O(n+m). TutorialsPoint - Decode the Message | Hash Map Substitution Approach
Asked in
Google 15 Meta 12 Amazon 8
28.5K Views
Medium Frequency
~15 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen