Given a string str, print of all the combinations of a string in lexicographical order.
Input: str = "ABC" Output: A AB ABC AC ACB B BA BAC BC BCA C CA CAB CB CBA Input: ED Output: D DE E ED
Approach: Count the occurrences of all the characters in the string using a map, then using recursion all the possible combinations can be printed. Store the elements and their counts in two different arrays. Three arrays are used, input array which has the characters, count array has the count of characters and result is a temporary array which is used in recursion to generate all the combinations. Using recursion and backtracking all the combinations can be printed.
Below is the implementation of the above approach.
A AB ABC AC ACB B BA BAC BC BCA C CA CAB CB CBA
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all longest common sub-sequences in lexicographical order
- Print all distinct circular strings of length M in lexicographical order
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Print all lexicographical greater permutations of a given string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Generate all numbers up to N in Lexicographical Order
- Sort the words in lexicographical order in Python
- Print a number as string of 'A' and 'B' in lexicographic order
- K-th lexicographical string of given length
- Lexicographical Maximum substring of string
- Lexicographical concatenation of all substrings of a string
- Largest lexicographical string with at most K consecutive elements
- Print all combinations of factors (Ways to factorize)
- Print all possible combinations of words from Dictionary using Trie
- Print all possible combinations of r elements in a given array of size n
- Combinations in a String of Digits
- Print all permutations in sorted (lexicographic) order
- Program to print numbers from N to 1 in reverse order
- Print Strings In Reverse Dictionary Order Using Trie
- Print array elements in alternatively increasing and decreasing order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.