
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Permutations of a Given String Using STL
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.
- Related Articles
- All reverse permutations of an array using STL in C++?
- C Program to print all permutations of a given string
- Print all permutations of a given string
- C++ Program to Find the Number of Permutations of a Given String
- All permutations of a string using iteration?
- Print all distinct permutations of a given string with duplicates in C++
- Python Program to print all permutations of a given string
- Print all the palindromic permutations of given string in alphabetic order in C++
- Print all palindrome permutations of a string in C++
- How to find all possible permutations of a given string in Python?
- Find the Number of Unique Permutations Starting with 1 of a Binary String using C++
- C program to find permutations of given strings
- Print prime numbers in a given range using C++ STL
- Counts of distinct consecutive sub-string of length two using C++ STL
- Print all permutations of a string in Java
