forked from navneet-bhole95/Code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added code to find the permutation of a string
- Loading branch information
1 parent
b70b838
commit b04cdcb
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |