Print all combinations generated by characters of a numeric string which does not exceed N | Java 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
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 ) ; } }