Problem · String

Repeated Grouped Digit Sum

Learn this problem
EasyTiktokOA
See Tiktok hiring insights

Problem statement

You are given a non-negative integer number represented as a string of digits and a positive integer k. Repeatedly perform the following algorithm:

  1. If the length of number is at most k, stop.
  2. Split number from left to right into groups of k digits. The final group may contain fewer than k digits.
  3. Compute the sum of the digits in each group and concatenate the decimal representations of those sums in the same order.
  4. Replace number with the concatenated result and return to step 1.

Return the resulting string number when the algorithm stops. It is guaranteed that the algorithm eventually terminates.

A solution with time complexity no worse than O(number.length * NUMBER_OF_STEPS) fits within the execution time limit.

Function

repeatedGroupedDigitSum(number: String, k: int) → String

Examples

Example 1

number = "11111222322"k = 3return = "144"
  1. Split 11111222322 into 111, 112, 223, and 22. Their digit sums are 3, 4, 7, and 4, producing 3474.
  2. Split 3474 into 347 and 4. Their digit sums are 14 and 4, producing 144.
  3. The length of 144 is 3, which is at most k, so return 144.

More Tiktok problems

drafts saved locally
public String repeatedGroupedDigitSum(String number, int k) {
  // write your code here
}
number"11111222322"
k3
expected"144"
checking account