Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Partition String Into Substrings With Values at Most K

XPChallenge Points: 20
levelLevel: Medium

You are given a numeric string s where every character is a digit from 1 to 9, and an integer k.

Your task is to divide s into multiple contiguous substrings such that:

  • Every character is used exactly once.

  • When each substring is converted to an integer, its value does not exceed k.

Among all valid ways to divide the string, return the smallest number of substrings required.
If it is impossible to divide the string under these rules, return -1.

Example 1:

Input: s = "314159", k = 70

Output: 3

Explanation:

Possible partition: "31", "41", "59" All values ≤ 70, total substrings = 3.

Example 2:

Input: s = "9999", k = 900

Output: 2

Explanation:

Any substring with more than 1 digit exceeds 900, but single-digit values 9 still exceed k → no valid partition.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result