Problem · String

Sum Consecutive Identical Digits

Learn this problem
EasyPayPayFULLTIMEOA

Problem 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":

  1. The leftmost pair 99 becomes 18, producing 18823.
  2. The leftmost pair 88 becomes 16, producing 11623.
  3. The leftmost pair 11 becomes 2, producing 2623.

No identical adjacent digits remain, so return "2623".

Complete sumConsecutiveIdenticalDigits, which takes digits and returns the final string.

Function

sumConsecutiveIdenticalDigits(digits: String) → String

Examples

Example 1

digits = "99823"return = "2623"

  • 99 becomes 18 → 18823
  • 88 becomes 16 → 11623
  • 11 becomes 2 → 2623
Now there are no more consecutive identical digits, so output the final result: 2623.

More PayPay problems

drafts saved locally
public String sumConsecutiveIdenticalDigits(String digits) {
  // write your code here
}
digits"99823"
expected"2623"
checking account