Katkat

Aggregate Letter-Number Groups

RiotgamesSoftware Engineer · Technical Screen

Last updated

This problem tests string parsing, data aggregation using a hash map, and sorting techniques.

Problem Statement

You are given a string containing segments of letters followed by numbers (e.g., "A10B20A5"). Your task is to parse this string, aggregate the values associated with each unique letter, and return a list of strings representing the sums, sorted alphabetically by the letter.

Function Signature

def aggregate_groups(input_string: str) -> list[str]

Rules

  1. Each segment consists of a single uppercase English letter followed by one or more digits.
  2. You must sum the numbers for each unique letter.
  3. The output should be a list of strings in the format "LetterTotal", sorted alphabetically by letter.

Examples

Example 1: Input: "A10B20A5" Output: ["A15", "B20"] Explanation: There are two 'A' groups (10 and 5) summing to 15, and one 'B' group (20).

Example 2: Input: "Z100X10Y5Z5" Output: ["X10", "Y5", "Z105"] Explanation: 'Z' appears twice (100+5=105), 'X' once (10), and 'Y' once (5).

Constraints

  • The string length is between 1 and 1000 characters.
  • The string is guaranteed to be well-formed (starts with a letter followed by numbers).
  • Digits can represent numbers larger than 9 (e.g., "A100").

Edge Cases

  • Single letter-number pair.
  • Multiple segments of the same letter appearing non-contiguously.
  • Numbers with multiple digits.

Hints

Hint 1

Use a hash map (dictionary) to store the running total for each letter encountered.

Hint 2

Iterate through the string, identifying the letter first, then extract the complete numerical value following it until the next letter or end of string.

Hint 3

After aggregating all totals into the dictionary, extract the keys, sort them, and format the final strings.

Follow-up questions

  • How would your approach change if the input string could contain invalid characters or malformed segments?
  • What is the time and space complexity of your solution in terms of string length N and number of unique letters K?
  • How would you modify the function if you were asked to return the output sorted by the sum in descending order instead of alphabetically?