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:
- [1345]
- [13, 45], [1, 345]
- [1, 3, 45]
- [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++
#include<bits/stdc++.h>
using namespace std;
int count1 = 0;
vector<string> split(string str)
{
vector<string> ans;
string word = "" ;
for ( auto x : str)
{
if (x == ' ' )
{
ans.push_back(word);
word = "" ;
}
else
{
word = word + x;
}
}
ans.push_back(word);
return ans;
}
bool check(string m)
{
if (m.length() == 1)
{
return true ;
}
vector<string> temp = split(m);
int number[temp.size()];
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
{
return false ;
}
}
return true ;
}
void print1(string m, int index, string ans)
{
if (index == m.length())
{
if (check(ans))
{
count1++;
}
return ;
}
print1(m, index + 1, ans + m[index]);
if (index != 0)
print1(m, index + 1,
ans + " " + m[index]);
}
int main()
{
string k = "1345" ;
print1(k, 0, "" );
cout << count1;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int count = 0 ;
static boolean check(String m)
{
if (m.length() == 1 ) {
return true ;
}
String temp[] = m.split( " " );
int number[] = new int [temp.length];
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 {
return false ;
}
}
return true ;
}
static void print(String m,
int index, String ans)
{
if (index == m.length()) {
if (check(ans)) {
++count;
}
return ;
}
print(m, index + 1 , ans + m.charAt(index));
if (index != 0 )
print(m, index + 1 ,
ans + " " + m.charAt(index));
}
public static void main(String[] args)
{
String k = Integer.toString( 1345 );
print(k, 0 , "" );
System.out.println(count);
}
}
|
Python3
count = 0
def check(m):
if ( len (m) = = 1 ):
return True
temp = m.split( " " )
number = [ 0 ] * ( len (temp))
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 :
return False
return True
def Print (m, index, ans):
global count
if (index = = len (m)):
if (check(ans)):
count + = 1
return
Print (m, index + 1 , ans + m[index])
if (index ! = 0 ):
Print (m, index + 1 , ans + " " + m[index])
k = "1345"
Print (k, 0 , "")
print (count)
|
C#
using System;
public class GFG {
static int count = 0;
static bool check(String m)
{
if (m.Length == 1) {
return true ;
}
String[] temp = m.Split( " " );
int [] number = new int [temp.Length];
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 {
return false ;
}
}
return true ;
}
static void print(String m, int index, String ans)
{
if (index == m.Length) {
if (check(ans)) {
++count;
}
return ;
}
print(m, index + 1, ans + m[index]);
if (index != 0)
print(m, index + 1, ans + " " + m[index]);
}
static public void Main()
{
String k = "1345" ;
print(k, 0, "" );
Console.WriteLine(count);
}
}
|
Javascript
<script>
let count = 0;
function check(m)
{
if (m.length == 1)
{
return true ;
}
let temp = m.split( " " );
let number = new Array(temp.length);
for (let i = 0; i < temp.length; ++i)
{
number[i] = parseInt(temp[i]);
}
let first = number[0];
for (let i = 1; i < number.length; ++i)
{
if (number[i] > first)
{
first = number[i];
}
else
{
return false ;
}
}
return true ;
}
function print(m, index, ans)
{
if (index == m.length)
{
if (check(ans))
{
++count;
}
return ;
}
print(m, index + 1, ans + m[index]);
if (index != 0)
print(m, index + 1,
ans + " " + m[index]);
}
let k = "1345" ;
print(k, 0, "" );
document.write(count);
</script>
|
Time Complexity: O(N*2N) where N is the length of string S
Auxiliary Space: O(1)