C++ Permutations of a Given String Using STL

C++Server Side ProgrammingProgramming

A permutation of a string is formed when the character of the given strings are rearranged in any form. In this tutorial, we are going to discuss how we can print all the permutations of a given string using C++’s Standard Template Library, for example

Input : s = “ADT”

Output : “ADT”, “ATD”, “DAT”, “DTA”, “TAD”, “TDA”

Explanation : In the given output as you can see all the string are made up of same three character present in our string and are just rearranged thus they fit in the definition of a permutation of a string now there is one more thing to note these are all the permutations possible of string s.

There are two methods by which we can print all the permutations of a given string

Rotate()

The first method we are going to use is by using the rotate method. In this method, we will use the rotate function of STL, which is used to rotate our string, and we are going to use recursion for printing the permutations.

Example

C++ Code for the Above method 

#include<bits/stdc++.h>
using namespace std;
void permutations(string s, string ans){
    if(s.size() == 0) {
// when our string which needs to
//be rotated becomes empty then it means
//that our permutation is stored in ans
        cout << ans << "\n";
        return ;
    }
    for(int i = 0; i < s.size(); i++){
        permutations(s.substr(1), ans + s[0]);
        // we are adding the
        // first character in our ans
        // passing all elements from index 1 in our
        // rotate string for next function.
        rotate(s.begin(), s.begin()+1, s.end());
        //rotating such that our second element becomes first
    }
}
int main(){
    string s = "ADT"; // given string
    permutations(s, "");
    return 0;
}

Output

ADT
ATD
DTA
DAT
TAD
TDA

Next_Permutation

Now we will use another function of STL, namely as next_Permutation as the name suggests, this function's return turn is whether the next permutation of this string exists or not. If no, then it returns false.

As you know, this function checks the next permutation; thus, we need first to sort the string lexicographically so that we get all the possible permutations.

Example

C++ Code for the Above Method

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s = "ADT"; // given string
    sort(s.begin(), s.end()); // sorting the string
    do{
        cout << s << "\n"; // printing the permutations
    }while(next_permutation(s.begin(), s.end())); // till next_permutations returns false
    return 0;
}

Output

ADT
ATD
DAT
DTA
TAD
TDA

In the above program, we sort our string, and then with the help of the next_permutation function, we print all the possible permutations.

Conclusion

In this tutorial, we print all the possible permutations of the given string using the help of STL in C++. We also learned the C++ program for this problem and some essential STL functions and their uses. We hope you find this tutorial helpful.

raja
Updated on 25-Nov-2021 10:24:01

Advertisements