Given an input string of numbers, find all combinations of numbers that can be formed using digits in the same order.
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 current index in given input string and length of 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 current digit to output. From here make two calls, one considering next digit as part of next number(including a space in output string) and one considering next digit as part of current number( no space included). If there are no digits remaining after current digit the second call to the function is omitted because a trailing space doesn’t count as a new combination.
1 2 1 4 1 2 14 1 21 4 1 214 12 1 4 12 14 121 4 1214
1214 1 214 12 14 1 2 14 121 4 1 21 4 12 1 4 1 2 1 4
This article is contributed by aditi sharma 2. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- itertools.combinations() module in Python to print all possible combinations
- Print all the combinations of a string in lexicographical order
- Combinations with repetitions
- Make all combinations of size k
- All unique combinations whose sum equals to K
- Problem on permutations and combinations | Set 2
- Find all combinations of two equal sum subsequences
- Iterating over all possible combinations in an Array using Bits
- Iterative approach to print all combinations of an Array
- Print all possible combinations of r elements in a given array of size n
- Print all combinations of factors (Ways to factorize)
- Combinations from n arrays picking one element from each array
- Print all the combinations of N elements by changing sign such that their sum is divisible by M
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Minimum length of string having all permutation of given string.