VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-ways-to-partition-a-number-into-increasing-sequences-of-digits/

⇱ Count ways to partition a number into increasing sequences of digits - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count ways to partition a number into increasing sequences of digits

Last Updated : 23 Jul, 2025

Given a numeric string S, the task is to find the number of ways to partition a string into substrings consisting of digits in increasing order.

Examples:

Input: S = "1345"
Output: 5
Explanation: Possible partitions are as follows: 

  1. [1345]
  2. [13, 45], [1, 345]
  3. [1, 3, 45]
  4. [1, 3, 4, 5]

Input: S = "12"
Output: 2

Approach: This problem can be solved by observing that between each digit either it will be a part of the previous number or it will be a new number so to solve the problem recursion can be used. Follow the steps below to solve the problem:

  • Initialize an integer variable, say count as 0, to store the number of ways to partition a string into increasing subsets.
  • Declare a function print() with index(storing current position), string S(given string in the question), and string ans( as parameters.
  • Now, following two cases are required to be considered: 
    • If S[index] is inserted in the previous number, then append S[index] at the end of ans and recall the function print() with parameters index + 1, S, and ans.
    • If S[index] is not a part of the previous number, then append " "(space) at the end of ans and then insert S[index] and recall the function print() with parameters index + 1, S, ans.
  • If index = S.length(), then check if  the digits in the sequences formed are in increasing order or not. If the sequences formed are increasing, increase count by 1.
  • Print count as the answer after performing the above steps.

Below is the implementation of the above approach:


Output: 
5

 

Time Complexity: O(N*2N) where N is the length of string S
Auxiliary Space: O(1)


 

Comment