Open in App
Not now

# Count ways to partition a number into increasing sequences of digits

• Difficulty Level : Expert
• Last Updated : 20 Jun, 2022

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:

## C++

 `// C++ program for the above approach` `#include` `using` `namespace` `std;`   `// Stores the number of ways` `// to partition a string` `int` `count1 = 0;` `vector split(string str)` `{` `    ``vector ans; ` `    ``string word = ``""``;` `    ``for``(``auto` `x : str) ` `    ``{` `        ``if` `(x == ``' '``)` `        ``{` `            ``ans.push_back(word);` `            ``word = ``""``;` `        ``}` `        ``else` `        ``{` `            ``word = word + x;` `        ``}` `    ``}` `    ``ans.push_back(word);` `    ``return` `ans;` `}`   `// Function to check if a sequence` `// is strictly increasing or not` `bool` `check(string m)` `{` `    `  `    ``// If there is only one number` `    ``if` `(m.length() == 1)` `    ``{` `        ``return` `true``;` `    ``}` `    `  `    ``// Split the string m when there is space` `    ``vector temp = split(m);` `    ``int` `number[temp.size()];` `    `  `    ``// Insert all the splits into the array` `    ``for``(``int` `i = 0; i < temp.size(); ++i) ` `    ``{` `        ``number[i] = stoi(temp[i]);` `    ``}` `    `  `    ``int` `first = number[0];` `    ``for``(``int` `i = 1; i < temp.size(); ++i)` `    ``{` `        ``if` `(number[i] > first)` `        ``{` `            ``first = number[i];` `        ``}` `        ``else` `        ``{` `            `  `            ``// If number is not increasing` `            ``return` `false``;` `        ``}` `    ``}` `    `  `    ``// If the sequence is increasing` `    ``return` `true``;` `}`   `// Recursive function to partition` `// a string in every possible substrings` `void` `print1(string m, ``int` `index, string ans)` `{` `    `  `    ``// If index = m.length, check if ans` `    ``// forms an increasing sequence or not` `    ``if` `(index == m.length())` `    ``{` `    `  `        ``if` `(check(ans))` `        ``{` `        `  `            ``// Increment count by 1,` `            ``// if sequence is increasing` `            ``count1++;` `        ``}` `        ``return``;` `    ``}   `   `    ``// If S[index] is appended to previous number` `    ``print1(m, index + 1, ans + m[index]);` `    `  `    ``if` `(index != 0)` `    `  `        ``// If S[index] is starting a new number` `        ``print1(m, index + 1,` `              ``ans + ``" "` `+ m[index]);` `}`   `// Driver Code` `int` `main()` `{` `    `  `    ``// Given Input` `    ``string k = ``"1345"``;` `    `  `    ``// Function Call` `    ``print1(k, 0, ``""``);` `    `  `    ``// Print the answer.` `    ``cout << count1;` `}`   `// This code is contributed by ipg2016107`

## Java

 `// Java program for the above approach`   `import` `java.io.*;` `import` `java.util.*;` `class` `GFG {`   `    ``// Stores the number of ways` `    ``// to partition a string` `    ``static` `int` `count = ``0``;`   `    ``// Function to check if a sequence` `    ``// is strictly increasing or not` `    ``static` `boolean` `check(String m)` `    ``{` `        ``// If there is only one number` `        ``if` `(m.length() == ``1``) {` `            ``return` `true``;` `        ``}`   `        ``// Split the string m when there is space` `        ``String temp[] = m.split(``" "``);` `        ``int` `number[] = ``new` `int``[temp.length];`   `        ``// Insert all the splits into the array` `        ``for` `(``int` `i = ``0``; i < temp.length; ++i) {`   `            ``number[i] = Integer.parseInt(temp[i]);` `        ``}`   `        ``int` `first = number[``0``];` `        ``for` `(``int` `i = ``1``; i < number.length; ++i) {`   `            ``if` `(number[i] > first) {` `                ``first = number[i];` `            ``}` `            ``else` `{`   `                ``// If number is not increasing` `                ``return` `false``;` `            ``}` `        ``}`   `        ``// If the sequence is increasing` `        ``return` `true``;` `    ``}`   `    ``// Recursive function to partition` `    ``// a string in every possible substrings` `    ``static` `void` `print(String m,` `                      ``int` `index, String ans)` `    ``{` `        ``// If index = m.length, check if ans` `        ``// forms an increasing sequence or not` `        ``if` `(index == m.length()) {`   `            ``if` `(check(ans)) {`   `                ``// Increment count by 1,` `                ``// if sequence is increasing` `                ``++count;` `            ``}` `            ``return``;` `        ``}`   `        ``// If S[index] is appended to previous number` `        ``print(m, index + ``1``, ans + m.charAt(index));` `        ``if` `(index != ``0``)` `            ``// If S[index] is starting a new number` `            ``print(m, index + ``1``,` `                  ``ans + ``" "` `+ m.charAt(index));` `    ``}`   `    ``// DriverCode` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``// Given Input` `        ``String k = Integer.toString(``1345``);`   `        ``// Function Call` `        ``print(k, ``0``, ``""``);`   `        ``// Print the answer.` `        ``System.out.println(count);` `    ``}` `}`

## Python3

 `# Python3 program for the above approach` `count ``=` `0` ` `  `# Function to check if a sequence` `# is strictly increasing or not` `def` `check(m):` `  `  `    ``# If there is only one number` `    ``if` `(``len``(m) ``=``=` `1``):` `        ``return` `True`   `    ``# Split the string m when there is space` `    ``temp ``=` `m.split(``" "``)` `    ``number ``=` `[``0``]``*``(``len``(temp))`   `    ``# Insert all the splits into the array` `    ``for` `i ``in` `range``(``len``(temp)):` `        ``number[i] ``=` `int``(temp[i])`   `    ``first ``=` `number[``0``]` `    ``for` `i ``in` `range``(``1``, ``len``(number)):` `        ``if` `(number[i] > first):` `            ``first ``=` `number[i]` `        ``else``:` `            ``# If number is not increasing` `            ``return` `False` `    ``# If the sequence is increasing` `    ``return` `True`   `# Recursive function to partition` `# a string in every possible substrings` `def` `Print``(m, index, ans):` `    ``global` `count` `    `  `    ``# If index = m.length, check if ans` `    ``# forms an increasing sequence or not` `    ``if` `(index ``=``=` `len``(m)):` `        ``if` `(check(ans)):` `          `  `            ``# Increment count by 1,` `            ``# if sequence is increasing` `            ``count``+``=``1` `        ``return`   `    ``# If S[index] is appended to previous number` `    ``Print``(m, index ``+` `1``, ans ``+` `m[index])` `    ``if` `(index !``=` `0``):` `      `  `        ``# If S[index] is starting a new number` `        ``Print``(m, index ``+` `1``, ans ``+` `" "` `+` `m[index])`   `# Given Input` `k ``=` `"1345"` ` `  `# Function Call` `Print``(k, ``0``, "")` ` `  `# Print the answer.` `print``(count)`   `# This code is contributed by suresh07.`

## C#

 `using` `System;`   `public` `class` `GFG {` `    ``static` `int` `count = 0;`   `    ``// Function to check if a sequence` `    ``// is strictly increasing or not` `    ``static` `bool` `check(String m)` `    ``{` `        ``// If there is only one number` `        ``if` `(m.Length == 1) {` `            ``return` `true``;` `        ``}`   `        ``// Split the string m when there is space` `        ``String[] temp = m.Split(``" "``);` `        ``int``[] number = ``new` `int``[temp.Length];`   `        ``// Insert all the splits into the array` `        ``for` `(``int` `i = 0; i < temp.Length; ++i) {`   `            ``number[i] = ``int``.Parse(temp[i]);` `        ``}`   `        ``int` `first = number[0];` `        ``for` `(``int` `i = 1; i < number.Length; ++i) {`   `            ``if` `(number[i] > first) {` `                ``first = number[i];` `            ``}` `            ``else` `{`   `                ``// If number is not increasing` `                ``return` `false``;` `            ``}` `        ``}`   `        ``// If the sequence is increasing` `        ``return` `true``;` `    ``}`   `    ``// Recursive function to partition` `    ``// a string in every possible substrings` `    ``static` `void` `print(String m, ``int` `index, String ans)` `    ``{` `        ``// If index = m.length, check if ans` `        ``// forms an increasing sequence or not` `        ``if` `(index == m.Length) {`   `            ``if` `(check(ans)) {`   `                ``// Increment count by 1,` `                ``// if sequence is increasing` `                ``++count;` `            ``}` `            ``return``;` `        ``}`   `        ``// If S[index] is appended to previous number` `        ``print(m, index + 1, ans + m[index]);` `        ``if` `(index != 0)` `            ``// If S[index] is starting a new number` `            ``print(m, index + 1, ans + ``" "` `+ m[index]);` `    ``}` `    ``static` `public` `void` `Main()` `    ``{`   `        ``String k = ``"1345"``;`   `        ``// Function Call` `        ``print(k, 0, ``""``);`   `        ``// Print the answer.` `        ``Console.WriteLine(count);` `    ``}` `}`   `// This code is contributed by maddler.`

## Javascript

 ``

Output:

`5`

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

My Personal Notes arrow_drop_up
Related Articles