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:
- Extract the cipher alphabet: Use the first appearance of each of the 26 lowercase letters in the
keyto create your substitution table - Map to regular alphabet: The first unique letter maps to 'a', second to 'b', and so on
- Decode the message: Replace each letter in the
messageusing your substitution table - 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
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!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code