LC 1309. Decrypt String from Alphabet to Integer Mapping

Nilanjan Deb · April 6, 2020

This is my CPP solution.

class Solution {
public:
    string freqAlphabets(string s) {
        int n = s.size();
        unordered_map<char,char> ai_map;
        for(int i=0;i<9;i++){
            ai_map['1'+i] = 'a'+i;
        }
        unordered_map<string,char> jz_map;
        for(int i=10;i<=26;i++){
            string res = to_string(i);
            res.push_back('#');
            jz_map[res] = 'a' + i -1;
        }
        string ans = "";
        int i = 0;
        while(i<n){
            if(i<n-2 && s[i+2] == '#'){
                string part(s.begin()+i,s.begin()+i+3);
                ans += jz_map[part];
                i+=3;
            }
            else{
                ans.push_back(ai_map[s[i]]);
                i++;
            }
        }
        return ans;
    }
};


Dicussion Forum