FastPrepTotal Each Letter's Count in an Encoded String
Problems/Total Each Letter's Count in an Encoded String

Total Each Letter's Count in an Encoded String

IBM logoIBM● EasyNEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: In an IBM new-grad online assessment with two coding questions in 75 minutes, the second question gave a string in a fixed format where each character is followed by a number. The task was to read every character with the number after it, add the numbers up per character, and output the totals in some order. The author warned that a number can have more than one digit.

The report does not give the exact format, the output order, or any limits or examples. Here, each pair is a lowercase letter followed by a count from 1 to 10^4 with no leading zeros, counts for the same letter are added, the answer lists each letter once in alphabetical order followed by its total, and the limits and examples are ours.

The judged task matches the report at about 75%.

You are given a string s made of pairs. Each pair is a lowercase English letter followed by a positive count written in decimal. A count can have more than one digit, and the same letter can appear in several pairs.

Add up the counts of each letter. Return one string built like this: for each distinct letter of s, in alphabetical order, write the letter followed by its total count in decimal without leading zeros. Each distinct letter appears once in the result, no matter how many pairs it has in s.

Function

aggregateLetterCounts(s: String) → String

Examples

Example 1

s = "a3c9b2c1"return = "a3b2c10"

The pairs are a3, c9, b2 and c1. Adding the counts per letter gives a = 3, b = 2 and c = 9 + 1 = 10. In alphabetical order the answer is a3b2c10.

Example 2

s = "z12a105z3"return = "a105z15"

The counts can have several digits: the pairs are z12, a105 and z3. So a = 105 and z = 12 + 3 = 15, and the answer is a105z15. Reading one digit at a time would wrongly treat z12 as z1 followed by a stray 2.

Example 3

s = "q7"return = "q7"

A single pair is already its own total.

Constraints

  • 2 <= s.length <= 2 * 10^4
  • s is a concatenation of pairs. Each pair is one lowercase English letter followed by a count from 1 to 10^4, written in decimal without leading zeros.

More IBM problems

  • Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
  • Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
  • Alternate Odd and Even Numbers with Two GoroutinesPHONE SCREEN · Seen Nov 2026
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public String aggregateLetterCounts(String s) {
        // Write your code here.
    }
}
Checking account…