Permutations with and without Repetition
Learn the difference between permutations with and without repetition.
We'll cover the following...
Reordering a string with repetition
Our next problem deals with generating permutations and combinations. How many ways can we rearrange a string? If repetition is allowed, the number will be large. How does this happen?
A string is a sequence of characters. It’s a representation of data structure, an array of characters.
Therefore, it has a length. If repetition is allowed, we can count the length of the string and safely say that the factorial of that number is the number of ways a string can be rearranged. But the real trick is how rearrangement works.
We get the following output when we run the code above:
abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdca cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba
The code above implements a recursive algorithm to generate all possible permutations of a given string with repetitions allowed.
-
Line 10–27: It defines a static method called
arrangeTheStringWithRepetition()that takes two string parameters:anyString, which represents the remaining characters to be arranged, and ...