LeetCode 1309: Decrypt String from Alphabet to Integer Mapping Solution

Master LeetCode problem 1309 (Decrypt String from Alphabet to Integer Mapping), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1309. Decrypt String from Alphabet to Integer Mapping

Easy

Problem Explanation

Explanation

To solve this problem, we can iterate through the given string s from right to left. If we encounter a #, we know the next two characters represent a letter. If not, the current character represents a letter. We can then map the corresponding letter and append it to our result string. We continue this process until we have processed all characters in s.

  • Time complexity: O(n), where n is the length of the input string s.
  • Space complexity: O(n) for storing the result string.

Solution Code

class Solution {
    public String freqAlphabets(String s) {
        StringBuilder result = new StringBuilder();
        int i = s.length() - 1;
        while (i >= 0) {
            if (s.charAt(i) == '#') {
                int num = Integer.parseInt(s.substring(i - 2, i));
                result.insert(0, (char) ('a' + num - 1));
                i -= 3;
            } else {
                result.insert(0, (char) ('a' + (s.charAt(i) - '0' - 1)));
                i--;
            }
        }
        return result.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1309 (Decrypt String from Alphabet to Integer Mapping)?

This page provides optimized solutions for LeetCode problem 1309 (Decrypt String from Alphabet to Integer Mapping) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1309 (Decrypt String from Alphabet to Integer Mapping)?

The time complexity for LeetCode 1309 (Decrypt String from Alphabet to Integer Mapping) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1309 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1309 in Java, C++, or Python.

Back to LeetCode Solutions