![]() |
VOOZH | about |
Suppose we have a string of length- n and we want to generate all taken r at a time repetitions. There are four fundamental concepts in Combinatorics
1) Combinations without repetitions/replacements.
2) Combinations with repetitions/replacements.
3) Permutations without repetitions/replacements.
4) Permutations with repetitions/replacements.
Below is a summary table depicting the fundamental concepts in Combinatorics Theory.
| nr possibilities https://www.geeksforgeeks.org/dsa/print-all-combinations-of-given-length/ See the special case when r=n below https://www.geeksforgeeks.org/dsa/print-all-permutations-with-repetition-of-characters/ | nPr possibilities https://www.geeksforgeeks.org/dsa/write-a-c-program-to-print-all-permutations-of-a-given-string/ Here r=n, as we are permuting all the characters of the string. |
| n+r-1Cr possibilities Current Article ( https://www.geeksforgeeks.org/dsa/combinations-with-repetitions/ ) | nCr possibilities https://www.geeksforgeeks.org/dsa/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ |
This article is about the third case(Order Not important and Repetitions allowed).
The idea is to recur for all the possibilities of the string, even if the characters are repeating.
The base case of the recursion is when there is a total of ‘r’ characters and the combination is ready to be printed.
For clarity, see the recursion tree for the string- “ 1 2 3 4” and r=2
Below is the implementation.
Output :
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Time Complexity: For a string of length- n and combinations taken r at a time with repetitions, it takes a total of O(n+r-1Cr) time.
References- https://en.wikipedia.org/wiki/Combination