Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
You are given a string s formed by digits and '#'. We want to map s to English lowercase characters as follows:
'a' to 'i') are represented by ('1' to '9') respectively.'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.
Example 1:
Input: s = "10#11#12" Output: "jkab" Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
Example 2:
Input: s = "1326#" Output: "acz"
Constraints:
1 <= s.length <= 1000s consists of digits and the '#' letter.s will be a valid string such that mapping is always possible.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"10#11#12"
"1326#"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1309: Decrypt String from Alphabet to Integer Mapping
class Solution {
public String freqAlphabets(String s) {
int i = 0, n = s.length();
StringBuilder ans = new StringBuilder();
while (i < n) {
if (i + 2 < n && s.charAt(i + 2) == '#') {
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 2)) - 1));
i += 3;
} else {
ans.append((char) ('a' + Integer.parseInt(s.substring(i, i + 1)) - 1));
i++;
}
}
return ans.toString();
}
}
// Accepted solution for LeetCode #1309: Decrypt String from Alphabet to Integer Mapping
func freqAlphabets(s string) string {
var ans []byte
for i, n := 0, len(s); i < n; {
if i+2 < n && s[i+2] == '#' {
num := (int(s[i])-'0')*10 + int(s[i+1]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 3
} else {
num := int(s[i]) - '0'
ans = append(ans, byte(num+int('a')-1))
i += 1
}
}
return string(ans)
}
# Accepted solution for LeetCode #1309: Decrypt String from Alphabet to Integer Mapping
class Solution:
def freqAlphabets(self, s: str) -> str:
ans = []
i, n = 0, len(s)
while i < n:
if i + 2 < n and s[i + 2] == "#":
ans.append(chr(int(s[i : i + 2]) + ord("a") - 1))
i += 3
else:
ans.append(chr(int(s[i]) + ord("a") - 1))
i += 1
return "".join(ans)
// Accepted solution for LeetCode #1309: Decrypt String from Alphabet to Integer Mapping
impl Solution {
pub fn freq_alphabets(s: String) -> String {
let s = s.as_bytes();
let mut ans = String::new();
let mut i = 0;
let n = s.len();
while i < n {
if i + 2 < n && s[i + 2] == b'#' {
let num = (s[i] - b'0') * 10 + (s[i + 1] - b'0');
ans.push((96 + num) as char);
i += 3;
} else {
let num = s[i] - b'0';
ans.push((96 + num) as char);
i += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #1309: Decrypt String from Alphabet to Integer Mapping
function freqAlphabets(s: string): string {
const ans: string[] = [];
for (let i = 0, n = s.length; i < n; ) {
if (i + 2 < n && s[i + 2] === '#') {
ans.push(String.fromCharCode(96 + +s.slice(i, i + 2)));
i += 3;
} else {
ans.push(String.fromCharCode(96 + +s[i]));
i++;
}
}
return ans.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.