Decrypt String from Alphabet to Integer Mapping

easy string

Problem

You are given a string s of digits and '#'. Map it to lowercase letters where '1'..'9' mean 'a'..'i' and '10#'..'26#' mean 'j'..'z'. Decode s by reading left to right: if the next two characters are followed by '#', take that two-digit code; otherwise take the single digit. Return the decoded string (the test guarantees a unique valid decoding).

Inputs = "10#11#12"
Output"jkab"
"10#"→j, "11#"→k, "1"→a, "2"→b.

def freq_alphabets(s):
    res = []
    i = 0
    while i < len(s):
        if i + 2 < len(s) and s[i + 2] == "#":
            num = int(s[i:i + 2])
            i += 3
        else:
            num = int(s[i])
            i += 1
        res.append(chr(ord("a") + num - 1))
    return "".join(res)
function freqAlphabets(s) {
  let res = "";
  let i = 0;
  while (i < s.length) {
    let num;
    if (i + 2 < s.length && s[i + 2] === "#") {
      num = parseInt(s.slice(i, i + 2), 10);
      i += 3;
    } else {
      num = parseInt(s[i], 10);
      i += 1;
    }
    res += String.fromCharCode(96 + num);
  }
  return res;
}
class Solution {
    public String freqAlphabets(String s) {
        StringBuilder res = new StringBuilder();
        int i = 0;
        while (i < s.length()) {
            int num;
            if (i + 2 < s.length() && s.charAt(i + 2) == '#') {
                num = Integer.parseInt(s.substring(i, i + 2));
                i += 3;
            } else {
                num = s.charAt(i) - '0';
                i += 1;
            }
            res.append((char) ('a' + num - 1));
        }
        return res.toString();
    }
}
string freqAlphabets(string s) {
    string res = "";
    int i = 0, n = s.size();
    while (i < n) {
        int num;
        if (i + 2 < n && s[i + 2] == '#') {
            num = (s[i] - '0') * 10 + (s[i + 1] - '0');
            i += 3;
        } else {
            num = s[i] - '0';
            i += 1;
        }
        res += (char) ('a' + num - 1);
    }
    return res;
}
Time: O(n) Space: O(n)