1309. Decrypt String from Alphabet to Integer Mapping LeetCode Solution

In this guide, you will get 1309. Decrypt String from Alphabet to Integer Mapping LeetCode Solution with the best time and space complexity. The solution to Decrypt String from Alphabet to Integer Mapping problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Decrypt String from Alphabet to Integer Mapping solution in C++
  4. Decrypt String from Alphabet to Integer Mapping solution in Java
  5. Decrypt String from Alphabet to Integer Mapping solution in Python
  6. Additional Resources
1309. Decrypt String from Alphabet to Integer Mapping LeetCode Solution image

Problem Statement of Decrypt String from Alphabet to Integer Mapping

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.

Example 1:

Input: s = “10#11#12”
Output: “jkab”
Explanation: “j” -> “10#” , “k” -> “11#” , “a” -> “1” , “b” -> “2”.

Example 2:

Input: s = “1326#”
Output: “acz”

Constraints:

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

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1309. Decrypt String from Alphabet to Integer Mapping LeetCode Solution in C++

class Solution {
 public:
  string freqAlphabets(string s) {
    string ans;

    for (int i = 0; i < s.length();) {
      if (i + 2 < s.length() && s[i + 2] == '#') {
        ans += stoi(s.substr(i, 2)) + 'a' - 1;
        i += 3;
      } else {
        ans += (s[i] - '0') + 'a' - 1;
        i += 1;
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1309. Decrypt String from Alphabet to Integer Mapping LeetCode Solution in Java

class Solution {
  public String freqAlphabets(String s) {
    String ans = "";

    for (int i = 0; i < s.length();) {
      if (i + 2 < s.length() && s.charAt(i + 2) == '#') {
        ans += (char) (Integer.valueOf(s.substring(i, i + 2)) + 'a' - 1);
        i += 3;
      } else {
        ans += (char) ((s.charAt(i) - '0') + 'a' - 1);
        i += 1;
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

1309. Decrypt String from Alphabet to Integer Mapping LeetCode Solution in Python

class Solution:
  def freqAlphabets(self, s: str) -> str:
    ans = ''
    i = 0

    while i < len(s):
      if i + 2 < len(s) and s[i + 2] == '#':
        ans += chr(int(s[i:i + 2]) + ord('a') - 1)
        i += 3
      else:
        ans += chr(int(s[i]) + ord('a') - 1)
        i += 1

    return ans
# code by PROGIEZ

Additional Resources

See also  137. Single Number II LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.