- Related Questions & Answers
- Print all distinct permutations of a given string with duplicates in C++
- Print all permutations in sorted (lexicographic) order in C++
- Print sorted distinct elements of array in C language
- Print all the duplicates in the input string in C++
- Print k different sorted permutations of a given array in C Program.
- Java Program to print distinct permutations of a string
- Find a Fixed Point in an array with duplicates allowed in C++
- Print first n distinct permutations of string using itertools in Python
- Print all permutations with repetition of characters in C++
- Insert Delete GetRandom O(1) - Duplicates allowed in C++
- Remove Duplicates from Sorted List in C++
- Count number of triplets with product equal to given number with duplicates allowed in C++
- Check if array contains contiguous integers with duplicates allowed in Python
- Remove Duplicates from Sorted Array II in C++
- Remove Duplicates from Sorted List II in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this programming problem, we are given a string and we have to print the distinct sorted permutations of the string elements that can be formed. The condition with this problem is that the string may contain a character that will arise more than one time. Also, the given string is inputted in sorted order.

Let’s take an example to understand the concept better,

Input : ABD Output : ABD , ADB , BAD , BDA , DAB , DBA INPUT : RSTU OUTPUT : RSTU , RSUT , RTSU , RTUS , RUST , RUTS , SRTU , SRUT , STRU , STUR , SURT , SUTR , TRSU , TRUS , TSRU , TSUR , TURS , TUSR , URST , URTS , USRT , USTR , UTRS , UTSR.

**PERMUTATION** is rearranging all the elements of a set based on a specific sequence or type. The set may be ordered or may not be ordered.

Now as we have learned everything about the problem. Let’s try to create a logic to solve the problem,

We know some mathematical formula related to permutations. The total number of strings that are generated by a string that contains n characters and all of the are distinct is given by n!. If there are characters in the string that are repeating itself then, the number of strings is given by **n! / i!**.

For string STURS, the total number of string that can be generated is 5! / 2! = 60. Since s occurs 2 times in the string.

Using this we know the number of string created. Now, we have to create those strings. For this first we will sort the string if not (string inputted will be sorted) so that the final string will be in a sorted form. Now, fix the first character of the string and find the permutation of all the rest and so on. This will give all the required permutations in a sorted manner.

**For example,**

**Input** − RST

**Logic **−

From this string total 3! = 6 permutations can be formed.

Let’s fix R and find permutations from s and t. This will give 2 strings **RST, RTS.**

Similarly fixing S will give **SRT, STR.** And fixing T will give **TRS, TSR.**

So, this will give the output as - **RST, RTS, SRT, STR, TRS, TSR.** which are in sort order.

Now, let's create a program to solve this problem,

#include <bits/stdc++.h> using namespace std; bool swaper(char str[], int start, int curr){ for (int i = start; i < curr; i++) if (str[i] == str[curr]) return 0; return 1; } void printPermutations(char str[], int index, int n){ if (index >= n) { cout<<str<<"\t"; return; } for (int i = index; i < n; i++) { bool check = swaper(str, index, i); if (check) { swap(str[index], str[i]); printPermutations(str, index + 1, n); swap(str[index], str[i]); } } } int main(){ char str[] = "AABC"; int n = strlen(str); cout<<"The string is : "<<str<<end; cout<<"The distinct sorted permutations are : \t"; printPermutations(str, 0, n); return 0; }

The string is : AABC The distinct sorted permutations are : AABC AACB ABAC ABCA ACBA ACAB BAAC BACA BCAA CABA CAAB CBAA

Advertisements