Print all combinations generated by characters of a numeric string which does not exceed N | C ++ program for the above approach ; Store the current sequence of s ; Store the all the required sequences ; Function to print all sequences of S satisfying the required condition ; Print all strings in the set ; Function to generate all sequences of string S that are at most N ; Iterate over string s ; Push ith character to combination ; Convert the string to number ; Check if the condition is true ; Push the current string to the final set of sequences ; Recursively call function ; Backtrack to its previous state ; Driver Code ; Function Call ; Print required sequences
#include <bits/stdc++.h> NEW_LINE 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 ; }