Problem · String

Repeatedly Add Consecutive Equal Digits

Learn this problem
EasyUber logoUberINTERNOA
See Uber hiring insights

Problem statement

See the Image Source section at the very bottom of the page for the original problem statement 🐼

Imagine you're given a string made entirely of digits, and your goal is to transform it by repeatedly applying a special operation whenever there are consecutive identical digits. Here's how it works: if a number has repeated digits, you replace each group of consecutive digits with their sum. For example, the number "999433" would become "2746" because 9 + 9 + 9 equals 27, and 3 + 3 equals 6. You keep doing this until there are no consecutive digits left. For instance, "44488366664" would first transform to "12163244." Keep applying this process until the string no longer has any repeating digits, and that's your final result. The challenge here is to find an approach that gets this done efficiently, though it doesn't need to be the most optimal one.

🐰 Appreciation to Charlotte baby 🐰

Function

repeatedlyAddConsecutiveEqualDigits(number: String) → String

Examples

Example 1

number = "999433"return = "2746"
Because 9 + 9 + 9 = 27 and 3 + 3 = 6

Example 2

number = "44488366664"return = "12163244"
Following the same logic mentioned above, you will get --> 12163244

Example 3

number = "66644319333"return = "26328"
Imagine you’re given a number with consecutive digits that are the same, and your task is to transform it step by step. Let’s take the number "66644319333." First, you spot the consecutive equal digits: "666," "44," and "333." You replace each group with the sum of its digits: "666" becomes 18, "44" becomes 8, and "333" becomes 9. So, the number now transforms into "1883199." But wait, there are still consecutive equal digits, "88" and "99," so we apply the same process again. This time, "88" becomes 16, and "99" becomes 18, turning the number into "1163118." Once more, there are consecutive digits, "11" and another "11." Summing those gives us "26328," which is the final result, with no more repeats left. So, the final output is "26328."

Example 4

number = "0044886"return = "084"
. After one operation, number = "08166". Then it sequentially becomes 08112 P.S. The explanation isn't complete. I'll add more once I find more reliable source :)

Constraints

🍓🍓

More Uber problems

drafts saved locally
public String repeatedlyAddConsecutiveEqualDigits(String number) {
  // write your code here
}
number"999433"
expected"2746"
checking account