Print all combinations generated by characters of a numeric string which does not exceed N
Last Updated :
11 May, 2021
Given a numeric string S of length M and an integer N, the task is to find all distinct combinations of S (repetitions allowed) that are at most N.
Examples:
Input: S = “124”, N = 100
Output: 1, 11, 12, 14, 2, 21, 22, 24, 4, 41, 42, 44
Explanation: Combinations “111”, “112”, “122”, “124”, “412” are greater than 100. Therefore, these combinations are excluded from the output.
Input: S = “345”, N = 400
Output: 3, 33, 333, 334, 335, 34, 343, 344, 345, 35, 353, 354, 355, 4, 43, 44, 45, 5, 53, 54, 55
Approach: The idea is to generate all the numbers possible using Backtracking and then print those numbers which does not exceed N. Follow the steps below to solve the problem:
- Initialize a Set of strings, say combinations[], to store all distinct combinations of S that numerically does not exceed N.
- Initialize a string ans to store the current combination of numbers possible from S.
- Declare a function generateCombinations() to generate all required combinations whose values are less than the given value N and the function is defined as:
- Traverse the string S over the range [0, M] using the variable i and do the following:
- Push the current character S[i] to ans and convert the current string ans to the number and store it in x.
- If x is less than or equal to N then push the string ans into combinations[] and recursively call the function generateCombinations().
- Backtrack to its previous state by removing the ith character from ans.
- After completing the above steps, print the set of all strings stored in combinations[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string combination;
set<string> combinations;
void printSequences(
set<string> combinations)
{
for (string s : combinations) {
cout << s << ' ' ;
}
}
void generateCombinations(string& s, int n)
{
for ( int i = 0; i < s.size(); i++) {
combination.push_back(s[i]);
long x = stol(combination);
if (x <= n) {
combinations.insert(combination);
generateCombinations(s, n);
}
combination.pop_back();
}
}
int main()
{
string S = "124" ;
int N = 100;
generateCombinations(S, N);
printSequences(combinations);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String combination = "" ;
static HashSet<String> combinations = new LinkedHashSet<String>();
static void printSequences(
HashSet<String> combinations)
{
for (String s : combinations)
{
System.out.print(s + " " );
}
}
static void generateCombinations(String s, int n)
{
for ( int i = 0 ; i < s.length(); i++)
{
combination += (s.charAt(i));
long x = Integer.valueOf(combination);
if (x <= n)
{
combinations.add(combination);
generateCombinations(s, n);
}
combination = combination.substring(
0 , combination.length() - 1 );
}
}
public static void main(String[] args)
{
String S = "124" ;
int N = 100 ;
generateCombinations(S, N);
printSequences(combinations);
}
}
|
Python3
combination = "";
combinations = [];
def printSequences(combinations) :
for s in (combinations) :
print (s, end = ' ' );
def generateCombinations(s, n) :
global combination;
for i in range ( len (s)) :
combination + = s[i];
x = int (combination);
if (x < = n) :
combinations.append(combination);
generateCombinations(s, n);
combination = combination[: - 1 ];
if __name__ = = "__main__" :
S = "124" ;
N = 100 ;
generateCombinations(S, N);
printSequences(combinations);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static String combination = "" ;
static SortedSet<String> combinations = new SortedSet<String>();
static void printSequences(
SortedSet<String> combinations)
{
foreach (String s in combinations)
{
Console.Write(s + " " );
}
}
static void generateCombinations(String s, int n)
{
for ( int i = 0; i < s.Length; i++)
{
combination += (s[i]);
long x = Int32.Parse(combination);
if (x <= n)
{
combinations.Add(combination);
generateCombinations(s, n);
}
combination = combination.Substring(
0, combination.Length - 1);
}
}
public static void Main(String[] args)
{
String S = "124" ;
int N = 100;
generateCombinations(S, N);
printSequences(combinations);
}
}
|
Javascript
<script>
var combination = "" ;
var combinations = [];
function printSequences(combinations) {
for ( var s of combinations) {
document.write(s + " " );
}
}
function generateCombinations(s, n) {
for ( var i = 0; i < s.length; i++) {
combination += s[i];
var x = parseInt(combination);
if (x <= n) {
combinations.push(combination);
generateCombinations(s, n);
}
combination = combination.substring(0, combination.length - 1);
}
}
var S = "124" ;
var N = 100;
generateCombinations(S, N);
printSequences(combinations);
</script>
|
Output:
1 11 12 14 2 21 22 24 4 41 42 44
Time Complexity: O(NN)
Auxiliary Space: O(NN)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...