Ctrl K

Merge Letter Counts

Medium
A string was compressed by writing each letter followed by how many times it repeats in a row. For example, "aaabacc" becomes "a3b1a1c2". You are given such a record in packed. Compress it better: every letter must appear in the record exactly once, with its total count, and the letters must be in alphabetical order. The original order of characters is lost, and that is intended. Return the new record.
Example 1
packed='b2a5b10' → 'a5b12'

The letter b appears twice: 2 + 10 = 12. Sorted alphabetically, that gives "a5b12".

Example 2
packed='c3a1c1a2' → 'a3c4'

a: 1 + 2 = 3, c: 3 + 1 = 4.

Example 3
packed='z1' → 'z1'
Constraints
  • 1 ≤ len(packed) ≤ 6·10⁴
  • packed is well formed: a lowercase English letter followed by a number
  • Every number is between 1 and 10⁴ with no leading zeros
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCharacters Grouped by Count MediumMost Searched Words MediumNo Twin Letters Side by Side MediumRanking Finalists by Jury Ballots MediumShuffle and Relabel Medium
loading Python…
Input
packed='b2a5b10'
Expected
'a5b12'

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.