Problem · String

Aggregate Values by Letter

Learn this problem
EasyRiot Games logoRiot GamesFULLTIMEPHONE SCREEN

Problem statement

You are given a string encoded made of alternating uppercase English letters and positive decimal integers. A letter may appear more than once.

Sum all values associated with each letter. Return one string containing every letter that appears, in alphabetical order, immediately followed by its total.

For example, the two tokens B3 and B9 contribute B12 to the result.

Function

aggregateLetterValues(encoded: String) → String

Examples

Example 1

encoded = "A12B3C32B9"return = "A12B12C32"

The totals are A = 12, B = 3 + 9 = 12, and C = 32. Writing the letters alphabetically produces A12B12C32.

Example 2

encoded = "Z5"return = "Z5"

Only Z appears, so its value is returned unchanged.

Example 3

encoded = "C10A1C7B3A4"return = "A5B3C17"

The repeated tokens give A = 1 + 4 and C = 10 + 7. The alphabetical order is A, B, then C.

Constraints

  • 2 ≤ encoded.length ≤ 200000.
  • encoded contains one or more alternating tokens, each formed by one uppercase English letter followed by one or more decimal digits.
  • Every token value is in [1, 10^9] and has no leading zero.
  • Every per-letter total fits in a signed 64-bit integer.
drafts saved locally
public String aggregateLetterValues(String encoded) {
    // Write your code here.
}
encoded"A12B3C32B9"
expected"A12B12C32"
checking account