#1309
Easy Algorithms Decrypt string from alphabet to integer mapping
String
80.5% acceptance
Feb 25, 2026
1606
120
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn freq_alphabets(s: String) -> String {
let bytes = s.as_bytes();
let n = bytes.len();
let mut res = String::new();
let mut i = 0;
while i < n {
if i + 2 < n && bytes[i + 2] == b'#' {
let num = (bytes[i] - b'0') as usize * 10 + (bytes[i + 1] - b'0') as usize;
res.push((b'a' + (num - 1) as u8) as char);
i += 3;
} else {
let num = (bytes[i] - b'0') as usize;
res.push((b'a' + (num - 1) as u8) as char);
i += 1;
}
}
res
}
}