Problem · String
Sum Consecutive Identical Digits
Learn this problemProblem statement
Given a string digits consisting only of decimal digits, repeatedly combine adjacent identical digits until no such pair remains.
At each step, choose the leftmost pair of identical adjacent digits. Replace that pair with the decimal representation of the sum of their numeric values, then restart the search from the beginning of the updated string.
For digits = "99823":
- The leftmost pair
99becomes18, producing18823. - The leftmost pair
88becomes16, producing11623. - The leftmost pair
11becomes2, producing2623.
No identical adjacent digits remain, so return "2623".
Complete sumConsecutiveIdenticalDigits, which takes digits and returns the final string.
Function
sumConsecutiveIdenticalDigits(digits: String) → StringExamples
Example 1
digits = "99823"return = "2623"
- 99 becomes 18 → 18823
- 88 becomes 16 → 11623
- 11 becomes 2 → 2623