Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many unique strings is possible with set amount of characters and length?

Tags:

math

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?

like image 248
Marwelln Avatar asked Oct 23 '13 08:10

Marwelln


People also ask

How do you count unique characters in a string?

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.

How do you find the number of possible combinations of a string?

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!

How do you count unique characters in a string in python?

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.


1 Answers

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!

like image 87
templatetypedef Avatar answered Nov 15 '22 07:11

templatetypedef