Decode the Message - Problem

You're a cryptographer tasked with decoding secret messages! 🕵️‍♀️

Given a key string and an encoded message, your job is to decode the message using a substitution cipher. Here's how the cipher works:

  1. Extract the cipher alphabet: Use the first appearance of each of the 26 lowercase letters in the key to create your substitution table
  2. Map to regular alphabet: The first unique letter maps to 'a', second to 'b', and so on
  3. Decode the message: Replace each letter in the message using your substitution table
  4. Preserve spaces: Spaces remain unchanged

Example: If key = "happy boy", the unique letters in order are: h, a, p, y, b, o
This creates the mapping: h→a, a→b, p→c, y→d, b→e, o→f

Goal: Return the fully decoded message as a string.

Input & Output

example_1.py — Basic Decoding
$ Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
Output: "this is a secret"
💡 Note: The key contains all 26 letters. First occurrence order: t→a, h→b, e→c, q→d, u→e, i→f, c→g, k→h, b→i, r→j, o→k, w→l, n→m, f→n, x→o, j→p, m→q, p→r, s→s, v→t, l→u, z→v, y→w, d→x, g→y. So 'vkbs' becomes 'this', 'bs' becomes 'is', etc.
example_2.py — Simple Case
$ Input: key = "happy boy", message = "lbkkz kt nlhr"
Output: "sunny is cool"
💡 Note: Key has unique letters: h→a, a→b, p→c, y→d, b→e, o→f. The mapping shows that 'l' maps to 's', 'b' maps to 'u', 'k' maps to 'n', 'k' maps to 'n', 'z' maps to 'y', creating 'sunny is cool'.
example_3.py — Edge Case with Spaces
$ Input: key = "abc defghijklmnopqrstuvwxyz", message = "a b c"
Output: "a b c"
💡 Note: The key maps a→a, b→b, c→c (first three letters map to themselves). The message 'a b c' decodes to 'a b c' with spaces preserved.

Constraints

  • 1 ≤ key.length ≤ 100
  • key consists of lowercase English letters and spaces
  • key contains every letter of the English alphabet at least once
  • 1 ≤ message.length ≤ 1000
  • message consists of lowercase English letters and spaces

Visualization

Tap to expand
🕵️ Secret Agent Decoder RingOuter Ring (Key)Inner Ring (Alphabet)hapyboabcdefDecoding Process:1. Build Mapping Table:h (1st unique) → aa (2nd unique) → bp (3rd unique) → cy (4th unique) → db (5th unique) → eo (6th unique) → f2. Decode "happy":h → aa → bp → cp → cy → dResult: "abccd"💡 Key Insight: Build the cipher table once, then use O(1) lookupsTime: O(key + message) | Space: O(1) - at most 26 mappings
Understanding the Visualization
1
Extract Unique Characters
From key 'happy boy', extract unique chars in order: h, a, p, y, b, o
2
Build Cipher Mapping
Map each unique char to alphabet: h→a, a→b, p→c, y→d, b→e, o→f
3
Decode Message
Use mapping to decode each character, preserving spaces
4
Return Result
Combine decoded characters into final secret message
Key Takeaway
🎯 Key Insight: The substitution cipher is like a decoder ring - once you align the rings properly (build the mapping table), decoding becomes a simple lookup operation for each character!
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 5
58.0K Views
Medium Frequency
~12 min Avg. Time
2.1K 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