Given a string of length n, print all permutation of the given string. Repetition of characters is allowed. Print these permutations in lexicographically sorted order
Input: AB Output: All permutations of AB with repetition are: AA AB BA BB Input: ABC Output: All permutations of ABC with repetition are: AAA AAB AAC ABA ... ... CCB CCC
For an input string of size n, there will be n^n permutations with repetition allowed. The idea is to fix the first character at first index and recursively call for other subsequent indexes. Once all permutations starting with the first character are printed, fix the second character at first index. Continue these steps till last character. Thanks to PsychoCoder for providing following C implementation.
All permutations with repetition of ABC are: AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC BBA BBB BBC BCA BCB BCC CAA CAB CAC CBA CBB CBC CCA CCB CCC
Fundamentals of Programming with C
Free Online Course By GeeksforGeeks
Following is recursion tree for input string “AB”. The purpose of recursion tree is to help in understanding the above implementation as it shows values of different variables.
data="" / \ / \ index=0 index=0 i=0 i=1 data="A" data="B" / \ / \ / \ / \ index=1 index=1 index=1 index=1 i=0 i=1 i=0 i=1 data="AA" data="AB" data="BA" data="BB"
In the above implementation, it is assumed that all characters of the input string are different. The implementation can be easily modified to handle the repeated characters. We have to add a preprocessing step to find unique characters (before calling allLexicographicRecur()).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- K length words that can be formed from given characters without repetition
- Distributing all balls without repetition
- Convert to a string that is repetition of a substring of k length
- Ways to sum to N using array elements with repetition allowed
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Write a program to print all permutations of a given string
- Print all permutations in sorted (lexicographic) order
- Print all distinct permutations of a given string with duplicates
- Print all palindrome permutations of a string
- Print all the palindromic permutations of given string in alphabetic order
- Print all permutations of a string in Java
- Iterative approach to print all permutations of an Array
- Print all permutations of a number N greater than itself
- Print all lexicographical greater permutations of a given string
- Minimum number of operations to move all uppercase characters before all lower case characters
- Find Kth largest string from the permutations of the string with two characters
- Count of distinct permutations of length N having no similar adjacent characters
- Print first n distinct permutations of string using itertools in Python
- Java Program to print distinct permutations of a string