# Distinct permutations of the string | Set 2

Print all distinct permutation of a string having duplicates.

Examples:

```Input : ABCA
Output : AABC AACB ABAC ABCA ACBA
ACAB BAAC BACA BCAA CABA
CAAB CBAA
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

An algorithm to print all distinct permutations has already been discussed here. Here we’ll discuss one more approach to do the same. Recall first how we print permutations without any duplicates in the input string. It is given here. Let’s now take the case of the string “ABAC”. While generating permutations, let’s say we are at index = 0, swap it with all elements after it. When we reach at i=2, we see that in the string s[index…i-1], there was an index which is equal to s[i]. Thus, swapping it will produce repeated permutations. Thus, we don’t swap it. The below explains it better.

Illustration : Let us understand with below example.

```i = 0 1 2 3
A B A C
index = 0, s = A
Start swapping s[index] with s[i] following it:
i = index + 1 = 1

Since s[index] != s[i], swap and recur.

i = 2, s[index] == s[i], don't swap

i = 3,  s[index] != s[i], swap and recur. ```

Below code does the same.

## C++

 `// C++ program to distinct permutations of the string ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if str[curr] does not matches with any of the ` `// characters after str[start] ` `bool` `shouldSwap(``char` `str[], ``int` `start, ``int` `curr) ` `{ ` `    ``for` `(``int` `i = start; i < curr; i++)  ` `        ``if` `(str[i] == str[curr]) ` `            ``return` `0; ` `    ``return` `1; ` `} ` ` `  `// Prints all distinct permutations in str[0..n-1] ` `void` `findPermutations(``char` `str[], ``int` `index, ``int` `n) ` `{ ` `    ``if` `(index >= n) { ` `        ``cout << str << endl; ` `        ``return``; ` `    ``} ` ` `  `    ``for` `(``int` `i = index; i < n; i++) { ` ` `  `        ``// Proceed further for str[i] only if it  ` `        ``// doesn't match with any of the characters ` `        ``// after str[index] ` `        ``bool` `check = shouldSwap(str, index, i); ` `        ``if` `(check) { ` `            ``swap(str[index], str[i]); ` `            ``findPermutations(str, index + 1, n); ` `            ``swap(str[index], str[i]); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``char` `str[] = ``"ABCA"``; ` `    ``int` `n = ``strlen``(str); ` `    ``findPermutations(str, 0, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to distinct permutations of the string ` `public` `class` `GFG { ` ` `  `// Returns true if str[curr] does not matches with any of the  ` `// characters after str[start]  ` `    ``static` `boolean` `shouldSwap(``char` `str[], ``int` `start, ``int` `curr) { ` `        ``for` `(``int` `i = start; i < curr; i++) { ` `            ``if` `(str[i] == str[curr]) { ` `                ``return` `false``; ` `            ``} ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `// Prints all distinct permutations in str[0..n-1]  ` `    ``static` `void` `findPermutations(``char` `str[], ``int` `index, ``int` `n) { ` `        ``if` `(index >= n) { ` `            ``System.out.println(str); ` `            ``return``; ` `        ``} ` ` `  `        ``for` `(``int` `i = index; i < n; i++) { ` ` `  `            ``// Proceed further for str[i] only if it  ` `            ``// doesn't match with any of the characters  ` `            ``// after str[index]  ` `            ``boolean` `check = shouldSwap(str, index, i); ` `            ``if` `(check) { ` `                ``swap(str, index, i); ` `                ``findPermutations(str, index + ``1``, n); ` `                ``swap(str, index, i); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``static` `void` `swap(``char``[] str, ``int` `i, ``int` `j) { ` `        ``char` `c = str[i]; ` `        ``str[i] = str[j]; ` `        ``str[j] = c; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) { ` ` `  `        ``char` `str[] = {``'A'``, ``'B'``, ``'C'``, ``'A'``}; ` `        ``int` `n = str.length; ` `        ``findPermutations(str, ``0``, n); ` `    ``} ` ` `  `} `

## Python3

 `# Python3 program to distinct ` `# permutations of the string  ` ` `  `# Returns true if str[curr] does not  ` `# matches with any of the characters ` `# after str[start]  ` `def` `shouldSwap(string, start, curr):  ` ` `  `    ``for` `i ``in` `range``(start, curr):  ` `        ``if` `string[i] ``=``=` `string[curr]:  ` `            ``return` `0` `    ``return` `1` ` `  `# Prints all distinct permutations ` `# in str[0..n-1]  ` `def` `findPermutations(string, index, n):  ` ` `  `    ``if` `index >``=` `n:  ` `        ``print``(''.join(string))  ` `        ``return` ` `  `    ``for` `i ``in` `range``(index, n):  ` ` `  `        ``# Proceed further for str[i] only  ` `        ``# if it doesn't match with any of  ` `        ``# the characters after str[index]  ` `        ``check ``=` `shouldSwap(string, index, i)  ` `        ``if` `check:  ` `            ``string[index], string[i] ``=` `string[i], string[index]  ` `            ``findPermutations(string, index ``+` `1``, n)  ` `            ``string[index], string[i] ``=` `string[i], string[index]  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``string ``=` `list``(``"ABCA"``)  ` `    ``n ``=` `len``(string)  ` `    ``findPermutations(string, ``0``, n)  ` `     `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# program to distinct permutations ` `// of the string ` `using` `System; ` ` `  `class` `GFG ` `{  ` ` `  `// Returns true if str[curr] does  ` `// not matches with any of the  ` `// characters after str[start]  ` `static` `bool` `shouldSwap(``char` `[]str,  ` `                       ``int` `start, ``int` `curr)  ` `{  ` `    ``for` `(``int` `i = start; i < curr; i++)  ` `    ``{  ` `        ``if` `(str[i] == str[curr])  ` `        ``{  ` `            ``return` `false``;  ` `        ``}  ` `    ``}  ` `    ``return` `true``;  ` `}  ` ` `  `// Prints all distinct permutations  ` `// in str[0..n-1]  ` `static` `void` `findPermutations(``char` `[]str,  ` `                             ``int` `index, ``int` `n)  ` `{  ` `    ``if` `(index >= n)  ` `    ``{  ` `        ``Console.WriteLine(str);  ` `        ``return``;  ` `    ``}  ` ` `  `    ``for` `(``int` `i = index; i < n; i++)  ` `    ``{  ` ` `  `        ``// Proceed further for str[i] only  ` `        ``// if it doesn't match with any of  ` `        ``// the characters after str[index]  ` `        ``bool` `check = shouldSwap(str, index, i);  ` `        ``if` `(check)  ` `        ``{  ` `            ``swap(str, index, i);  ` `            ``findPermutations(str, index + 1, n);  ` `            ``swap(str, index, i);  ` `        ``}  ` `    ``}  ` `}  ` ` `  `static` `void` `swap(``char``[] str, ``int` `i, ``int` `j) ` `{  ` `    ``char` `c = str[i];  ` `    ``str[i] = str[j];  ` `    ``str[j] = c;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `    ``char` `[]str = {``'A'``, ``'B'``, ``'C'``, ``'A'``};  ` `    ``int` `n = str.Length;  ` `    ``findPermutations(str, 0, n);  ` `}  ` `}  ` ` `  `// This code is contributed  ` `// by 29AjayKumar `

Output:

```ABCA
ABAC
ACBA
ACAB
AACB
AABC
BACA
BAAC
BCAA
CBAA
CABA
CAAB
```

This article is contributed by ekta1994. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.