Combinations in a String of Digits
Last Updated :
20 Feb, 2023
Given an input string of numbers, find all combinations of numbers that can be formed using digits in the same order.
Examples:
Input : 123
Output :1 2 3
1 23
12 3
123
Input : 1234
Output : 1 2 3 4
1 2 34
1 23 4
1 234
12 3 4
12 34
123 4
1234
The problem can be solved using recursion. We keep track of the current index in the given input string and the length of the output string so far. In each call to the function, if there are no digits remaining in the input string print the current output string and return. Otherwise, copy the current digit to output. From here make two calls, one considering the next digit as part of the next number(including a space in output string) and one considering the next digit as part of the current number( no space included). If there are no digits remaining after the current digit the second call to the function is omitted because a trailing space doesn’t count as a new combination.
C++
#include <iostream>
#include <cstring>
using namespace std;
void printCombinations( char * input, int index,
char * output, int outLength)
{
if (input[index] == '\0' )
{
output[outLength] = '\0' ;
cout << output << endl;
return ;
}
output[outLength] = input[index];
output[outLength + 1] = ' ' ;
printCombinations(input, index + 1, output,
outLength + 2);
if (input[index + 1] != '\0' )
printCombinations(input, index + 1, output,
outLength + 1);
}
int main()
{
char input[] = "1214" ;
char *output = new char [100];
output[0] = '\0' ;
printCombinations(input, 0, output, 0);
return 0;
}
|
Java
class GFG
{
static void printCombinations( char [] input,
int index,
char [] output,
int outLength)
{
if (input.length == index)
{
System.out.println(String.valueOf(output));
return ;
}
output[outLength] = input[index];
output[outLength + 1 ] = ' ' ;
printCombinations(input, index + 1 , output,
outLength + 2 );
if (input.length!=index + 1 )
printCombinations(input, index + 1 , output,
outLength + 1 );
}
public static void main(String[] args)
{
char input[] = "1214" .toCharArray();
char []output = new char [ 100 ];
printCombinations(input, 0 , output, 0 );
}
}
|
Python3
def printCombinations( input , index, output, outLength):
if ( len ( input ) = = index):
output[outLength] = '\0'
print ( * output[:outLength], sep = "")
return
output[outLength] = input [index]
output[outLength + 1 ] = ' '
printCombinations( input , index + 1 ,
output, outLength + 2 )
if ( len ( input ) ! = (index + 1 )):
printCombinations( input , index + 1 ,
output, outLength + 1 )
input = "1214"
output = [ 0 ] * 100
output[ 0 ] = '\0'
printCombinations( input , 0 , output, 0 )
|
C#
using System;
class GFG
{
static void printCombinations( char [] input,
int index,
char [] output,
int outLength)
{
if (input.Length == index)
{
Console.WriteLine(String.Join( "" ,
output));
return ;
}
output[outLength] = input[index];
output[outLength + 1] = ' ' ;
printCombinations(input, index + 1, output,
outLength + 2);
if (input.Length!=index + 1)
printCombinations(input, index + 1, output,
outLength + 1);
}
public static void Main(String[] args)
{
char []input = "1214" .ToCharArray();
char []output = new char [100];
printCombinations(input, 0, output, 0);
}
}
|
Javascript
<script>
function printCombinations(input,index,output,outLength)
{
if (input.length == index)
{
document.write(output.join( "" )+ "<br>" );
return ;
}
output[outLength] = input[index];
output[outLength + 1] = ' ' ;
printCombinations(input, index + 1, output,
outLength + 2);
if (input.length != index + 1)
printCombinations(input, index + 1, output,
outLength + 1);
}
let input = "1214" .split( "" );
let output = new Array(100);
printCombinations(input, 0, output, 0);
</script>
|
Output:
1 2 1 4
1 2 14
1 21 4
1 214
12 1 4
12 14
121 4
1214
Time complexity : O(2^n)
Space complexity : O(n^2)
Alternative Solution:
C++
#include <bits/stdc++.h>
using namespace std;
void printCombinations( char s[]){
int l = strlen (s);
for ( int i = 0; i < pow (2, l - 1); i++){
int k = i, x = 0;
cout << s[x];
x++;
for ( int j = 0; j < strlen (s) - 1; j++){
if (k & 1)
cout << " " ;
k = k >> 1;
cout << s[x];
x++;
}
cout << "\n" ;
}
}
int main() {
char input[] = "1214" ;
printCombinations(input);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printCombinations( char s[])
{
int l = s.length;
for ( int i = 0 ;
i < Math.pow( 2 , l - 1 ); i++)
{
int k = i, x = 0 ;
System.out.print(s[x]);
x++;
for ( int j = 0 ;
j < s.length - 1 ; j++)
{
if (k % 2 == 1 )
System.out.print( " " );
k = k >> 1 ;
System.out.print(s[x]);
x++;
}
System.out.print( "\n" );
}
}
public static void main(String[] args)
{
char input[] = "1214" .toCharArray();
printCombinations(input);
}
}
|
Python3
def printCombinations(s):
l = len (s);
for i in range ( pow ( 2 , l - 1 )):
k = i
x = 0
print (s[x], end = "")
x + = 1
for j in range ( len (s) - 1 ):
if (k & 1 ):
print ( " " , end = "")
k = k >> 1
print (s[x], end = "")
x + = 1
print ()
if __name__ = = "__main__" :
inp = "1214" ;
printCombinations(inp);
|
C#
using System;
class GFG
{
static void printCombinations( char []s)
{
int l = s.Length;
for ( int i = 0;
i < Math.Pow(2, l - 1); i++)
{
int k = i, x = 0;
Console.Write(s[x]);
x++;
for ( int j = 0;
j < s.Length - 1; j++)
{
if (k % 2 == 1)
Console.Write( " " );
k = k >> 1;
Console.Write(s[x]);
x++;
}
Console.Write( "\n" );
}
}
public static void Main(String[] args)
{
char []input = "1214" .ToCharArray();
printCombinations(input);
}
}
|
Javascript
<script>
function printCombinations(s)
{
let l = s.length;
for (let i = 0;
i < Math.pow(2, l - 1); i++)
{
let k = i, x = 0;
document.write(s[x]);
x++;
for (let j = 0;
j < s.length - 1; j++)
{
if (k % 2 == 1)
document.write( " " );
k = k >> 1;
document.write(s[x]);
x++;
}
document.write( "<br>" );
}
}
let input= "1214" .split( "" );
printCombinations(input);
</script>
|
Output:
1214
1 214
12 14
1 2 14
121 4
1 21 4
12 1 4
1 2 1 4
Time Complexity : O(n * 2^n)
Space Complexity : O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...