
- 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
Print all permutations with repetition of characters in C++
In this problem, we are given a string of n characters and we have to print all permutations of characters of the string. Repeating of characters of the string is allowed. The printing of permutation should be done in alphabetical order (lexicographically sorted order).
Let’s take an example to understand the topic better :
Input − XY
Output − XX, XY, YX, YY
To solve this problem, we need to use fix and recur logic. Here, we will fix one element at first index of the array and then recursively call for the next elements in the sequence.
Let’s see an implementation example which will make the solution clear to you.
Input string XY.
Fix first elements at 1 index: X_
Recursively call other elements and fill: XX -> XY
Now fix the next element at index1: Y_
Recursively call other elements and fill: YX-> YY
The same logic can be used for 3,4,n length string.
Example
#include <iostream> #include<string.h> using namespace std; void printPermutations(char *str, char* permutations, int last, int index){ int i, len = strlen(str); for ( i = 0; i < len; i++ ) { permutations[index] = str[i] ; if (index == last) cout<<permutations <<"\t"; else printPermutations (str, permutations, last, index+1); } } int main() { char str[] = "ABC"; cout<<"All permutations of the string with repetition of "<<str<<" are: "<<endl ; int len = strlen(str) ; char permutations[len]; printPermutations (str, permutations, len-1, 0); return 0; }
Output
All permutations of the string 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
- Related Articles
- Print all permutations of a string in Java
- Print all permutations of a given string
- Print all distinct permutations of a given string with duplicates in C++
- Print all palindrome permutations of a string in C++
- Print all permutations in sorted (lexicographic) order in C++
- Python Program to print all permutations of a given string
- C Program to print all permutations of a given string
- Print all the palindromic permutations of given string in alphabetic order in C++
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- Print all distinct characters of a string in order in C++
- Print distinct sorted permutations with duplicates allowed in input in C++
- All possible permutations of N lists in Python
- Generating all possible permutations of array in JavaScript
- Java Program to print distinct permutations of a string
