Partition String Into Substrings With Values at Most K - Problem
You're tasked with breaking down a string of digits into the minimum number of valid pieces. Given a string s containing only digits from 1 to 9 and an integer k, you need to partition the string such that:
  • Every digit belongs to exactly one substring
  • When each substring is interpreted as an integer, its value must be โ‰ค k
  • You want the fewest possible substrings
For example, if s = "165334" and k = 60, you could partition it as ["1", "6", "53", "3", "4"] (5 parts) or ["16", "53", "34"] (3 parts). The second partition is better! Return the minimum number of substrings needed, or -1 if no valid partition exists.

Input & Output

example_1.py โ€” Basic partition
$ Input: s = "165334", k = 60
โ€บ Output: 3
๐Ÿ’ก Note: We can partition as ["16", "53", "34"]. All values (16, 53, 34) are โ‰ค 60, giving us 3 partitions total.
example_2.py โ€” Impossible case
$ Input: s = "238", k = 4
โ€บ Output: -1
๐Ÿ’ก Note: No valid partition exists because even single digits like 8 exceed k=4, making it impossible to satisfy the constraint.
example_3.py โ€” Single character partitions
$ Input: s = "123", k = 1
โ€บ Output: 3
๐Ÿ’ก Note: Each digit must be its own partition: ["1", "2", "3"], since combining any two would exceed k=1.

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists only of digits from '1' to '9'
  • 1 โ‰ค k โ‰ค 109
  • No leading zeros in any partition

Visualization

Tap to expand
Greedy vs Exhaustive: s="165334", k=60Greedy Approach O(n)Step 1: "1" โ†’ "16" (extend)Step 2: "16" โ†’ "165" > 60 (cut)Partition 1: "16"Step 3: "5" โ†’ "53" (extend)Step 4: "53" โ†’ "533" > 60 (cut)Partition 2: "53"Step 5: "3" โ†’ "34" (extend)Step 6: End reachedPartition 3: "34"Result: 3 partitionsExhaustive Search O(2^n)Try: ["1","6","5","3","3","4"] = 6Try: ["1","6","5","3","34"] = 5Try: ["1","6","5","33","4"] = 5Try: ["1","6","53","3","4"] = 5Try: ["1","6","53","34"] = 4Try: ["16","5","3","3","4"] = 5Try: ["16","5","3","34"] = 4Try: ["16","5","33","4"] = 4Try: ["16","53","3","4"] = 4Try: ["16","53","34"] = 3 โœ“Result: 3 partitions๐ŸŽฏ Key Insight: Why Greedy WorksTaking the longest valid substring at each step is always optimal because:โ€ข Delaying a cut never makes future decisions worseโ€ข We want to minimize the number of partitions, so longer substrings are betterComplexity ComparisonGreedy: O(n) time, O(1) space - Single pass, immediate decisionsExhaustive: O(2^n) time, O(n) space - Must try all 2^(n-1) possible cuts
Understanding the Visualization
1
Greedy Path
Takes longest valid substring at each step
2
Exhaustive Search
Explores all possible partitions
3
Efficiency Gain
Greedy is O(n) vs O(2^n) for brute force
Key Takeaway
๐ŸŽฏ Key Insight: Greedy algorithm achieves optimal results in linear time by always choosing the longest valid substring, proving that being locally optimal leads to global optimality in this problem.
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 22
38.2K Views
Medium-High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen