Given a string of length n, print all permutations of the given string. Repetition of characters is allowed. Print these permutations in lexicographically sorted order
Examples:
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 the following C implementation.
C++
#include <bits/stdc++.h>
#include<string.h>
using namespace std;
int compare ( const void * a, const void * b);
void allLexicographicRecur ( char *str, char * data,
int last, int index)
{
int i, len = strlen (str);
for ( i = 0; i < len; i++ )
{
data[index] = str[i] ;
if (index == last)
cout << data << endl;
else
allLexicographicRecur (str, data, last, index+1);
}
}
void allLexicographic( char *str)
{
int len = strlen (str) ;
char *data = ( char *) malloc ( sizeof ( char ) * (len + 1)) ;
data[len] = '\0' ;
qsort (str, len, sizeof ( char ), compare);
allLexicographicRecur (str, data, len-1, 0);
free (data);
}
int compare ( const void * a, const void * b)
{
return ( *( char *)a - *( char *)b );
}
int main()
{
char str[] = "ABC" ;
cout << "All permutations with repetition of " <<
str << " are: " <<endl ;
allLexicographic(str);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int compare ( const void * a, const void * b);
void allLexicographicRecur ( char *str, char * data, int last, int index)
{
int i, len = strlen (str);
for ( i=0; i<len; i++ )
{
data[index] = str[i] ;
if (index == last)
printf ( "%s\n" , data);
else
allLexicographicRecur (str, data, last, index+1);
}
}
void allLexicographic( char *str)
{
int len = strlen (str) ;
char *data = ( char *) malloc ( sizeof ( char ) * (len + 1)) ;
data[len] = '\0' ;
qsort (str, len, sizeof ( char ), compare);
allLexicographicRecur (str, data, len-1, 0);
free (data);
}
int compare ( const void * a, const void * b)
{
return ( *( char *)a - *( char *)b );
}
int main()
{
char str[] = "ABC" ;
printf ( "All permutations with repetition of %s are: \n" ,
str);
allLexicographic(str);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static void allLexicographicRecur(String str, char [] data,
int last, int index)
{
int length = str.length();
for ( int i = 0 ; i < length; i++)
{
data[index] = str.charAt(i);
if (index == last)
System.out.println( new String(data));
else
allLexicographicRecur(str, data, last,
index + 1 );
}
}
static void allLexicographic(String str)
{
int length = str.length();
char [] data = new char [length + 1 ];
char [] temp = str.toCharArray();
Arrays.sort(temp);
str = new String(temp);
allLexicographicRecur(str, data, length - 1 , 0 );
}
public static void main(String[] args)
{
String str = "ABC" ;
System.out.printf( "All permutations with " +
"repetition of %s are: \n" , str);
allLexicographic(str);
}
}
|
Python3
def toString( List ):
return ''.join( List )
def allLexicographicRecur (string, data, last, index):
length = len (string)
for i in range (length):
data[index] = string[i]
if index = = last:
print (toString(data))
else :
allLexicographicRecur(string, data, last, index + 1 )
def allLexicographic(string):
length = len (string)
data = [""] * (length + 1 )
string = sorted (string)
allLexicographicRecur(string, data, length - 1 , 0 )
string = "ABC"
print ( "All permutations with repetition of " + string + " are:" )
allLexicographic(string)
|
C#
using System;
public class GFG
{
static void allLexicographicRecur(String str, char [] data,
int last, int index)
{
int length = str.Length;
for ( int i = 0; i < length; i++)
{
data[index] = str[i];
if (index == last)
Console.WriteLine( new String(data));
else
allLexicographicRecur(str, data, last,
index + 1);
}
}
static void allLexicographic(String str)
{
int length = str.Length;
char [] data = new char [length + 1];
char [] temp = str.ToCharArray();
Array.Sort(temp);
str = new String(temp);
allLexicographicRecur(str, data, length - 1, 0);
}
public static void Main(String[] args)
{
String str = "ABC" ;
Console.Write( "All permutations with " +
"repetition of {0} are: \n" , str);
allLexicographic(str);
}
}
|
Javascript
<script>
function allLexicographicRecur(str, data, last, index)
{
let length = str.length;
for (let i = 0; i < length; i++)
{
data[index] = str[i];
if (index == last)
document.write( new String(data) + "<br/>" );
else
allLexicographicRecur(str, data, last,
index + 1);
}
}
function allLexicographic(str)
{
let length = str.length;
let data = new Array(length + 1);
let temp = str.split();
temp.sort();
str = new String(temp);
allLexicographicRecur(str, data, length - 1, 0);
}
let str = "ABC" ;
document.write( "All permutations with " +
"repetition of " + str + " are " + "<br/>" );
allLexicographic(str);
</script>
|
OutputAll 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
Time Complexity: O(n^n)
Auxiliary Space: O(n)
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()).