Decrypt String from Alphabet to Integer Mapping in Python

Suppose we have a string s that contains digits ('0' - '9') and '#' characters. We need to map this string to English lowercase characters using a specific encoding scheme ?

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

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

For example, if the input is "10#11#12", the output will be "jkab" because 10# maps to 'j', 11# maps to 'k', 1 maps to 'a', and 2 maps to 'b'.

Approach

We'll solve this by processing the string from right to left and building a mapping dictionary ?

  • Create a mapping dictionary for all possible character codes

  • Process the string from right to left

  • If we encounter '#', read the two digits before it

  • Otherwise, read single digit

  • Build the result string using the mappings

Example

class Solution:
    def freqAlphabets(self, s):
        # Create mapping dictionary
        mapping = {}
        
        # Map single digits 1-9 to a-i
        for i in range(1, 10):
            mapping[str(i)] = chr(ord('a') + i - 1)
        
        # Map 10#-26# to j-z
        for i in range(10, 27):
            mapping[str(i)] = chr(ord('a') + i - 1)
        
        result = ""
        i = len(s) - 1
        
        # Process string from right to left
        while i >= 0:
            if s[i] == '#':
                # Extract two-digit number before #
                num = s[i-2:i]
                result = mapping[num] + result
                i -= 3  # Skip the number and #
            else:
                # Single digit
                result = mapping[s[i]] + result
                i -= 1
        
        return result

# Test the solution
solution = Solution()
print(solution.freqAlphabets("10#11#12"))
print(solution.freqAlphabets("1326#"))
print(solution.freqAlphabets("25#"))
jkab
acz
y

How It Works

The algorithm processes the encoded string from right to left ?

  1. Mapping Creation: We create a dictionary mapping each possible code to its corresponding letter

  2. Right-to-Left Processing: Starting from the end helps us identify multi-character codes easily

  3. Hash Detection: When we find '#', we know the previous two characters form a code

  4. Single Digit: Any other character is a single-digit code

Alternative Approach

Here's a more concise solution using string replacement ?

def decrypt_string(s):
    # Replace multi-character codes first (26# to 10#)
    for i in range(26, 9, -1):
        s = s.replace(f"{i}#", chr(ord('a') + i - 1))
    
    # Replace single digit codes (9 to 1)
    for i in range(9, 0, -1):
        s = s.replace(str(i), chr(ord('a') + i - 1))
    
    return s

# Test the alternative approach
print(decrypt_string("10#11#12"))
print(decrypt_string("1326#"))
jkab
acz

Comparison

Approach Time Complexity Space Complexity Best For
Right-to-Left Parsing O(n) O(1) Large strings
String Replacement O(n²) O(n) Short strings

Conclusion

The right-to-left parsing approach efficiently decodes the alphabet-to-integer mapping by processing characters from the end. This ensures we correctly identify multi-character codes marked with '#' and provides optimal O(n) time complexity.

Updated on: 2026-03-25T07:31:47+05:30

885 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements