If I have two characters (a, b
) and a length of three (aaa, aab ...
), how do I count how many unique strings I can make of that (and what is the math method called)?
Is this correct?
val = 1, amountCharacters = 2, length = 3;
for (i = 1; i <= length; ++i) { val = amountCharacters*val; uniqueStrings = val }
This example returns 8 which is correct. If I try with something higher, like amountCharacters = 10
it returns 1000. Is it still correct?
Approach: The given problem can be solved using the set data structure. The idea is to initialize an unordered set that stores all the distinct characters of the given string. The size of the set after the string is traversed is the required answer.
The number of combinations of n objects taken r at a time is determined by the following formula: C(n,r)=n! (n−r)! r!
Python3. If not present append the characters to empty string. Now the empty string consists of only unique characters, use len() method to display the length.
If you have n different characters and the length is k, there are exactlty nk possible strings you can form. Each character independently of the rest can be one of n different options and there are k total choices to make. Your code is correct.
For 2 possible characters and 10 letters, there are exactly 1024 possible strings.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With