Decrypt String from Alphabet to Integer Mapping - Problem

You are given a string s formed by digits and '#'. We want to map s to English lowercase characters as follows:

Characters ('a' to 'i') are represented by ('1' to '9') respectively.

Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.

Return the string formed after mapping.

The test cases are generated so that a unique mapping will always exist.

Input & Output

Example 1 — Basic Mixed Pattern
$ Input: s = "10#11#12"
Output: "jkab"
💡 Note: 10# maps to 'j' (10th letter), 11# maps to 'k' (11th letter), then we have '12' remaining without #, so these are processed as individual digits: 1→'a', 2→'b'. Final result: jkab
Example 2 — Only Single Digits
$ Input: s = "1326#"
Output: "acz"
💡 Note: 1→'a', 3→'c', 26#→'z' (26th letter). The digits 1 and 3 are single digits since they're not part of a ## pattern.
Example 3 — Only Hash Patterns
$ Input: s = "25#"
Output: "y"
💡 Note: 25# maps to 'y' (25th letter of alphabet). This is the simplest case with just one encoded character.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists only of digits and the '#' letter
  • s will be a valid string such that mapping is always possible

Visualization

Tap to expand
Decrypt String from Alphabet to Integer Mapping INPUT String s = "10#11#12" 1 0 # 1 1 # 1 2 0 1 2 3 4 5 6 7 Mapping Rules: '1'-'9' --> 'a'-'i' '10#'-'26#' --> 'j'-'z' 10# = 'j' 11# = 'k' 12 = 'l' ALGORITHM STEPS (Right to Left Scanning) 1 Start from right i = len(s) - 1 = 7 2 Check for '#' If '#', take 2 digits before 3 Convert to char chr(num + ord('a') - 1) 4 Build result Prepend char to result Trace (Right to Left): i=7: '2' --> skip (part of 12) i=6: '1' --> skip (part of 12) i=5: '#' --> "12"=12 --> 'l' i=2: '#' --> "11"=11 --> 'k' i=-1: '#' --> "10"=10 --> 'j' FINAL RESULT Decoded String: j k l Output: "jkl" Breakdown: "10#" --> 10 --> 'j' (10th) "11#" --> 11 --> 'k' (11th) "12" --> 12 --> 'l' (12th) OK - Complete Key Insight: Scanning from RIGHT to LEFT is crucial! When we encounter '#', we know the previous two characters form a two-digit number (10-26). This eliminates ambiguity since '#' acts as a clear marker. Time: O(n), Space: O(n) for the result string. Each character is processed exactly once. TutorialsPoint - Decrypt String from Alphabet to Integer Mapping | Right to Left Scanning Approach
Asked in
Amazon 25 Microsoft 18
25.0K Views
Medium Frequency
~15 min Avg. Time
892 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