Total Per-Letter Counts from an Encoded String with Multi-Digit Numbers
Company: Confluent
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Online Assessment
Overview: A string-parsing task where each lowercase letter is followed by a count that may have several digits, and letters can repeat. The function must total the counts per letter and return them in alphabetical order, testing careful tokenization of multi-digit numbers and hash-based aggregation.
Read the full Confluent Software Engineer interview experience this question came from
Constraints
- 2 <= len(s) <= 2 * 10^5
- s is well formed: a concatenation of one or more tokens, each a lowercase letter (a to z) immediately followed by a decimal integer of one or more digits with no leading zeros; s contains only lowercase letters and digits and starts with a letter.
- Every token's integer is between 1 and 10^4 inclusive.
- Every total is below 4 * 10^8, so it fits in a signed 32-bit integer.
Examples
Input: ('a3b12a2',)
Expected Output: 'a5b12'
Explanation: Source example 1: a appears in two non-adjacent tokens (3 + 2 = 5) and b12 is read as twelve.
Input: ('c1b10c9a100',)
Expected Output: 'a100b10c10'
Explanation: Source example 2: letters first appear as c, b, a but are listed alphabetically; b and c tie at 10; the zeros in 10 and 100 belong to the numbers.
Hints
- A number is the whole run of digits after its letter, so keep reading digits until the next letter or the end of the string.
- The same letter can appear in several tokens that are far apart, and its total combines all of them.
- The output order depends only on the letters themselves, not on where each first appears or how large its total is.