FastPrepEqual-Sum Digit Partitions

Equal-Sum Digit Partitions

Alchemy logoAlchemy● MediumFULLTIMEOA
Learn

Problem statement

Given a nonempty string digits containing decimal digits, insert zero or more underscores between adjacent digits. Keep a partition exactly when every contiguous segment has the same digit sum. Return all valid partition strings in standard lexicographic order.

The uncut input is always a valid one-segment partition. Leading zeroes remain part of their original segments.

Function

equalSumPartitions(digits: String) → String[]

Examples

Example 1

digits = "1741380"return = ["1741380","174_1380","17_413_80"]

The three valid cuts have common segment sums 24, 12, and 8 respectively.

Example 2

digits = "1111"return = ["1111","11_11","1_1_1_1"]

Constraints

  • 1 <= digits.length <= 16.
  • digits contains only characters 0 through 9.

More Alchemy problems

See Alchemy hiring insights
public String[] equalSumPartitions(String digits) {
  // write your code here
}
digits"1741380"
expected["1741380", "174_1380", "17_413_80"]
Checking account…