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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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 possible combinations of the string by replacing '$' with any other digit from the string
- 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
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.