All problems
0836HardMathStringBacktrackingGreedyNumber Theory

Least Serial Whose Digits Carry the Factor

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3348Smallest Divisible Digit Product II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A press stamps serial numbers. num is the serial it is about to stamp, written as a string of digits with no leading zero.

The digit product of a serial is what you get by multiplying all of its digits together. A serial is acceptable when it carries no digit 0 and its digit product is a multiple of t.

Return the smallest acceptable serial that is greater than or equal to num, as a string. The answer may hold more digits than num. If no acceptable serial exists at all, return the string "-1".

Examples

Example 1

Input
num = "15", t = 6
Output
"16"

The serial "16" carries no zero and its digit product is 6, which is a multiple of 6. No acceptable serial lies between 15 and 16.

Example 2

Input
num = "99", t = 11
Output
"-1"

A digit product is a product of single digits, so it can never carry the factor 11, and the answer is the string "-1".

Example 3

Input
num = "99", t = 49
Output
"177"

The factor 49 needs two sevens, and no two-digit serial at or above 99 supplies them, so the answer grows to three digits: "177" has digit product 49.

Constraints

  • 2 <= num.length <= 200000
  • num consists of digits only
  • num has no leading zero
  • 1 <= t <= 100000000000000

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def least_serial_reaching(num: str, t: int) -> str:
Java
public String leastSerialReaching(String num, long t)
September 7
Apply