Skip to content

Commit

Permalink
added code
Browse files Browse the repository at this point in the history
added code to find the permutation of a string
  • Loading branch information
peaceDude404 authored Oct 22, 2020
1 parent b70b838 commit b04cdcb
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// C++ program to print all permutations with
// duplicates allowed using rotate() in STL
#include <bits/stdc++.h>
using namespace std;

// Function to print permutations of string str,
// out is used to store permutations one by one
void permute(string str, string out)
{
// When size of str becomes 0, out has a
// permutation (length of out is n)
if (str.size() == 0)
{
cout << out << endl;
return;
}

// One be one move all characters at
// the beginning of out (or result)
for (int i = 0; i < str.size(); i++)
{
// Remove first character from str and
// add it to out
permute(str.substr(1), out + str[0]);

// Rotate string in a way second character
// moves to the beginning.
rotate(str.begin(), str.begin() + 1, str.end());
}
}

// Driver code
int main()
{
string str;
cin>> str;
permute(str, "");
return 0;
}

0 comments on commit b04cdcb

Please sign in to comment.