all permutations with repetition c 2b 2b

Solutions on MaxInterview for all permutations with repetition c 2b 2b by the best coders in the world

showing results for - "all permutations with repetition c 2b 2b"
Emely
25 Jan 2019
1#include <string>
2#include <iostream>
3
4
5void print_str(const char*,std::string,const int, const int);
6
7int main()
8
9{
10
11    int lenght = 2;
12
13    char str[] = {'A', 'B', 'C', 'D'};
14
15
16
17    int n = sizeof str;
18
19    print_str(str, "", n, lenght);  //Note: this function works on all cases and not just the case above
20
21    return 0;
22
23}
24
25// The main recursive method to print all possible strings of length "length"
26    void print_str(const char str[],std::string prefix,const int n, const int lenght)
27
28    {
29
30        if (lenght == 1)
31
32            {
33
34                for (int j = 0; j < n; j++)
35
36                std::cout << prefix + str[j] << std::endl;
37
38            }//Base case: lenght = 1, print the string "lenght" times + the remaining letter
39
40        else
41
42            {
43
44
45               // One by one add all characters from "str" and recursively call for "lenght" equals to "lenght"-1
46                for (int i = 0; i < n; i++)
47
48                // Next character of input added
49                print_str(str, prefix + str[i], n, lenght - 1);
50                // "lenght" is decreased, because we have added a new character
51
52            }
53
54    }
55