forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_permutations.cpp
54 lines (49 loc) · 1.34 KB
/
string_permutations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Problem: Print all the permutations of a string.
*
* Details: Permutation is "arrangement number" or "order". So problem is given a string, how many strings can we produce by rearranging them.
*
* Example: Permutations of ABC are ABC, ACB, BCA, BAC, CAB, CBA
*
* Approach: Lets take a backtracking approach. We select one letter, then recursively permute the remaining letters.
* At each step of permutation process, the given set will have two parts, a part we have already processed and the part we are yet to process.
* So, at ith step we exchange the i’th value with the value being chosen at that stage.
*
*/
#include <iostream>
#include <cstring>
/*
* Helper routine to swap pointers
*/
void swap( char *a, char *b )
{
char temp = *a;
*a = *b;
*b = temp;
}
/*
* Function : Permute
* Arg 1: c string
* Arg 2: starting index of the string.
* Arg 3: ending index of the string/
*/
void permute(char *str, int l, int r)
{
if (l == r)
{
std::cout << str << std::endl;
}
for (int i = l; i <= r; ++i)
{
swap( (str + l), (str + i) );
permute(str, l+1, r);
swap( (str + l), (str + i) );
}
}
int main()
{
char str[] = "abc";
std::cout << "Permutations of the string " << str << " are :\n";
permute(str, 0, std::strlen(str)-1);
return 0;
}