Decrypt String from Alphabet to Integer Mapping - Problem

You're a cryptographer who has intercepted an encoded message! ๐Ÿ•ต๏ธโ€โ™‚๏ธ

The message is a string s containing only digits and '#' symbols. Your mission is to decrypt it back to readable English lowercase letters using this special mapping:

  • Single digits ('1' to '9') represent letters ('a' to 'i') respectively
  • Two digits followed by '#' ('10#' to '26#') represent letters ('j' to 'z') respectively

Example: The encrypted string "10#11#12" decodes to "jkl" because:

  • 10# โ†’ 'j' (10th letter)
  • 11# โ†’ 'k' (11th letter)
  • 12# โ†’ 'l' (12th letter)

๐ŸŽฏ Your goal: Return the decrypted string. The input is guaranteed to have a unique valid decoding!

Input & Output

example_1.py โ€” Basic Mixed Encoding
$ Input: s = "10#11#12"
โ€บ Output: "jkl"
๐Ÿ’ก Note: 10# maps to 'j' (10th letter), 11# maps to 'k' (11th letter), 12# maps to 'l' (12th letter). No single digits in this example.
example_2.py โ€” Single Digits Only
$ Input: s = "1326#"
โ€บ Output: "acz"
๐Ÿ’ก Note: 1 maps to 'a' (1st letter), 3 maps to 'c' (3rd letter), 26# maps to 'z' (26th letter). Mixed single digits and one two-digit mapping.
example_3.py โ€” All Single Digits
$ Input: s = "123456789"
โ€บ Output: "abcdefghi"
๐Ÿ’ก Note: Each digit 1-9 maps directly to letters a-i respectively. No '#' symbols present, so all are single-digit mappings.

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s consists of digits and the '#' symbol
  • s will be a valid string such that mapping is always possible
  • The mapping is always unique (no ambiguous cases)

Visualization

Tap to expand
๐Ÿ” Secret Message DecoderDecoding: "10#11#12" โ†’ "jkl"Step 1: Identify the encrypted parts10#11#12#Step 2: Process from right to leftStep 3: Decode each part to letters10th letterj11th letterk12th letterlStep 4: Final decoded message"jkl"๐ŸŽฏ Mission accomplished! Secret message decoded successfully.
Understanding the Visualization
1
Identify the Pattern
Recognize that '#' marks the end of two-digit codes
2
Process Backwards
Start from the end and work backwards through the string
3
Decode Each Part
Convert numbers to their corresponding alphabet positions
4
Assemble Message
Build the final decoded message
Key Takeaway
๐ŸŽฏ Key Insight: By processing the string backwards, we can easily distinguish between single-digit codes (1-9) and two-digit codes (10#-26#) without any ambiguity!
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
95.0K Views
Medium Frequency
~15 min Avg. Time
2.8K 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