Total Each Letter's Count in an Encoded String
Problem statement
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) → StringExamples
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^4sis a concatenation of pairs. Each pair is one lowercase English letter followed by a count from1to10^4, written in decimal without leading zeros.