FastPrepK-Group Digit-Sum Compression

K-Group Digit-Sum Compression

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

Problem statement

While digits.length > k, split it into consecutive groups of length k, allowing a shorter final group. Replace each group by its decimal digit sum and concatenate the replacements.

Return the first string whose length is at most k.

Function

compressDigitGroups(digits: String, k: int) → String

Examples

Example 1

digits = "1111122222"k = 3return = "132"

Repeated group-sum passes end at 132.

Example 2

digits = "1111122222"k = 5return = "510"

The two length-five groups sum to 5 and 10, producing 510.

Constraints

  • 1 <= digits.length <= 100000
  • 2 <= k <= 100000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String compressDigitGroups(String digits, int k) {
    // Write your code here.
}
digits"1111122222"
k3
expected"132"
Checking account…