← Home

1309. Decrypt String from Alphabet to Integer Mapping

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
130

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1309. Decrypt String from Alphabet to Integer Mapping, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are freqAlphabets.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 63.30% of C++ online submissions for Decrypt String from Alphabet to Integer Mapping.
02//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Decrypt String from Alphabet to Integer Mapping.
03
04class Solution {
05public:
06    string freqAlphabets(string s) {
07        int cursor = 0;
08        string ans;
09        
10        while(cursor < s.size()){
11            if(s.find('#', cursor) - cursor != 2){
12                // -1 because 'a' maps to 1
13                ans += ('a' + (s[cursor] - '0' - 1));
14                // cout << cursor << ", " << (char)('a' + (s[cursor] - '0')) << endl;
15                cursor++;
16            }else{
17                // -1 because 'a' maps to 1
18                ans += ('a' + stoi(s.substr(cursor, 2)) - 1);
19                // cout << cursor << ", " << (char)('a' + stoi(s.substr(cursor, 2))) << endl;
20                cursor+=3;
21            }
22        }
23        
24        return ans;
25    }
26};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.