Compute letter frequencies from encoded string
Company: Oracle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string parsing and frequency aggregation, specifically handling mixed-length encodings, tokenization of digits and '#' markers, and parsing numeric repetition counts while respecting linear-time and constant-space constraints.
Constraints
- s is a valid encoding produced by the rules above.
- Single-letter codes are 1..9 (a..i); two-letter codes are 10#..26# (j..z).
- A repetition suffix (k) appears only after a letter code, with k >= 2; k may have multiple digits.
- Output is exactly 26 integers, index 0 = 'a' through index 25 = 'z'.
Examples
Input: ("1226#24#",)
Expected Output: [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]
Explanation: Decodes to "abzx": 1->a, 2->b, 26#->z, 24#->x.
Input: ("1(2)23(3)",)
Expected Output: [2, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation: Decodes to "aabccc": 1(2)->aa, 2->b, 3(3)->ccc.
Input: ("2110#(2)",)
Expected Output: [1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation: Decodes to "bajj": 2->b, 1->a, 10#(2)->jj.
Input: ("23#(2)24#25#26#23#(3)",)
Expected Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 1, 1]
Explanation: Decodes to "wwxyzwww": 23#(2)->ww, 24#->x, 25#->y, 26#->z, 23#(3)->www. Total w=5, x=1, y=1, z=1.
Input: ("",)
Expected Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation: Empty input decodes to the empty string; all counts are zero.
Input: ("1(12)26#",)
Expected Output: [12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
Explanation: Multi-digit repeat count: 1(12)->twelve a's, 26#->z.
Input: ("9",)
Expected Output: [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Explanation: Single single-digit letter: 9->i.
Hints
- Scan left to right. The only way to tell a two-digit letter (10#..26#) from a single digit is to peek two characters ahead for a '#'.
- After consuming a letter's code, check whether the next character is '('. If so, read the integer up to ')' — it can be more than one digit — and use it as the multiplier; otherwise the letter occurs once.
- Map the numeric value v (1..26) to array index v-1 and add the repetition count there.