-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path02_permutations.cpp
60 lines (47 loc) · 1021 Bytes
/
02_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
54
55
56
57
58
59
60
/*
Topic - Permutations
Given a string s, task is to find all permutation of the string s.
*/
#include <iostream>
using namespace std;
// function find all permutations
void permute(char *inp, int idx)
{
// Base Case
if(inp[idx] == '\0')
{
cout << inp << ", ";
return;
}
// Recursive Case
for(int j=idx; inp[j] != '\0'; j++)
{
swap(inp[idx], inp[j]);
permute(inp, idx+1);
// backtracing: to restore the original array
swap(inp[idx], inp[j]);
}
}
// function to drive code
int main()
{
char ch[10];
cout << "Enter your string: ";
cin >> ch;
cout << "Permutations: ";
permute(ch, 0);
cout << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter your string : abc
Permutations : abc, acb, bac, bca, cba, cab,
Case 2:
Enter your string : aba
Permutations : aba, aab, baa, baa, aba, aab,
Case 3:
Enter your string : 123
Permutations : 123, 132, 213, 231, 321, 312,
*/