Ctrl K

Digit Pieces Under a Cap

Medium
You are given a string digits of the digits 1 through 9 and an integer cap. Cut the string into consecutive pieces; the value of a piece is the number it spells out (for instance, the piece "35" has value 35). Every piece must have a value of at most cap. Return the smallest possible number of pieces, or -1 if no such cut exists.
Example 1
digits="381927", cap=50 → 3

For example "38", "19", "27": every value is at most 50. Two pieces are impossible, since one of them would have three digits.

Example 2
digits="472", cap=6 → -1

The digit 7 alone already exceeds 6, so no valid cut exists.

Example 3
digits="1111", cap=1111 → 1
Constraints
  • 1 ≤ len(digits) ≤ 10⁵
  • Every character of digits is a digit from 1 to 9
  • 1 ≤ cap ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBrackets With Wildcards MediumBead String Without Twins MediumAlternate After Rotating MediumWells Beside the Houses MediumBinary Number Under a Cap Medium
loading Python…
Input
digits="381927", cap=50
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.