Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #339 from rafu01/master
Browse files Browse the repository at this point in the history
Added all permutation of a string in C++
  • Loading branch information
SSKale1 committed Oct 2, 2020
2 parents 52567b3 + 6dfaa98 commit 70d3703
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions C++/Algorithms/BackTrackingAlgorithms/AllPermutationOfString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

/*
title: all permutation of a string
what will it do: It will generate all permutation of a string
time complexity: O(n*n!)
code written and tested by: https://github.com/rafu01
date: 10-oct-2020
*/

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

// 1st approach
// the idea is to keep one part of the string fixed and swap the other parts
void permutation(string s, int left, int right)
{
// Base case
if (left == right)
cout << s << endl;
else
{
// swapping to create the permuation
for (int i = left; i <= right; i++)
{
swap(s[left], s[i]);
permutation(s, left + 1, right);
swap(s[left], s[i]);
}
}
}
// 2nd approach
void permutationWithSTL(string s)
{
// We first sort the string, so that it is converted to lexicographically smallest permutation
// ascennding order
sort(s.begin(), s.end());
// Keep printing next permutation while there next permutation exists
do
{
cout << s << endl;
} while (next_permutation(s.begin(), s.end()));
}

int main()
{
string str = "ABC";
int n = str.size();
permutation(str, 0, n - 1);
permutationWithSTL(str);
return 0;
}

/*
Recursive tree for first function. string: ABC
ABC
/ | \
ABC BAC CBA --> A is fixed in ABC, B is fixed BAC, C is fiex in CBA
/ \ / \ / \
ABC ACB BAC BCA CBA CAB --> AB in ABC, AC in ACB, BA in BAC, BC in BCA, CB in CBA, CA in CAB are fixed
*/

0 comments on commit 70d3703

Please sign in to comment.