Quick 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.

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

A string encodes counts in a fixed format: a lowercase letter followed by a decimal number, repeated. The same letter may appear more than once. Add up the numbers for each letter and return a summary string that lists every letter that appears, in alphabetical order, each followed by its total. The candidate's report says only that the totals are output in order; this version fixes alphabetical order of the letters. A number can have more than one digit, so it is the whole run of digits after its letter, not a single character. ### Function Signature ```python def summarize_counts(s: str) -> str: ``` ### Rules - `s` is a concatenation of one or more tokens. Each token is one lowercase English letter (`a` to `z`) immediately followed by a decimal integer of one or more digits with no leading zeros. - A token's integer is the entire run of consecutive digits that follows its letter: in `b12a2`, the letter `b` carries 12, not 1. - A letter's total is the sum of the integers of all tokens with that letter. - Return the letters that appear at least once, in increasing alphabetical order, each immediately followed by its total written in decimal without leading zeros, all concatenated with no separators. ### Constraints - `2 <= len(s) <= 2 * 10^5` - `s` is well formed as described: it 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 **Example 1** ```text Input: s = "a3b12a2" Output: "a5b12" ``` The tokens are `a3`, `b12` and `a2`. The letter `a` totals `3 + 2 = 5` and `b` totals 12. **Example 2** ```text Input: s = "c1b10c9a100" Output: "a100b10c10" ``` The letter `c` totals `1 + 9 = 10`. The letters are listed as `a`, `b`, `c` regardless of where each first appears. **Example 3** ```text Input: s = "z9" Output: "z9" ```

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

A string `s` encodes counts in a fixed format: a lowercase letter followed by a decimal number, repeated. The same letter may appear more than once. Add up the numbers for each letter and return a summary string that lists every letter that appears, in alphabetical order, each followed by its total. Implement `summarize_counts(s)`. **Rules** - `s` is a concatenation of one or more tokens. Each token is one lowercase English letter (`a` to `z`) immediately followed by a decimal integer of one or more digits with no leading zeros. - A token's integer is the entire run of consecutive digits that follows its letter: in `b12a2`, the letter `b` carries 12, not 1. - A letter's total is the sum of the integers of all tokens with that letter. - Return the letters that appear at least once, in increasing alphabetical order (regardless of where each letter first appears), each immediately followed by its total written in decimal without leading zeros, all concatenated with no separators. **Constraints** - `2 <= len(s) <= 2 * 10^5` - `s` is well formed as described: it 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 (no value exceeds 2^31 - 1). **Example 1** ```text Input: s = "a3b12a2" Output: "a5b12" ``` The tokens are `a3`, `b12` and `a2`. The letter `a` totals 3 + 2 = 5 and `b` totals 12. **Example 2** ```text Input: s = "c1b10c9a100" Output: "a100b10c10" ``` The letter `c` totals 1 + 9 = 10. The letters are listed as `a`, `b`, `c` regardless of where each first appears.

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

  1. 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.
  2. The same letter can appear in several tokens that are far apart, and its total combines all of them.
  3. The output order depends only on the letters themselves, not on where each first appears or how large its total is.

Loading coding console...

Show the approach

Approach

Scan s once from left to right. At each letter, read the entire run of digits that follows it, stopping at the next letter or at the end of the string, and build that token's integer digit by digit. Add the integer to the letter's slot in a 26-entry table of totals and mark the letter as seen. Invariant: after each token is consumed, every slot holds the sum of the integers of all tokens of that letter read so far, and the index points at the next token's letter (or past the end). The inner loop runs until a non-digit or the end of the string, so the last token's digits are added like every other token's, and each token is added exactly once. Finally walk the table from 'a' to 'z' and append each seen letter followed by its total in decimal; index order is increasing alphabetical order, independent of where a letter first appears or how large its total is. Edge cases: a single token such as z9; a letter repeated in non-adjacent tokens; integers with internal or trailing zeros (101, 1010, 10000), which are read as whole numbers; sums that carry (9999 + 1 = 10000). Totals stay below 4 * 10^8, inside a signed 32-bit integer; the Java and C++ references accumulate in 64-bit anyway.

Time complexity:
O(n)
Space complexity:
O(1)