可以有多少个唯一的字符串

可以有多少个唯一的字符串

本文介绍了设定数量的字符和长度,可以有多少个唯一的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个字符(a, b),长度为三个字符(aaa, aab ...),我该如何计算我可以用多少个唯一的字符串(又叫什么数学方法)?

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)?

这正确吗?

val = 1, amountCharacters = 2, length = 3;
for (i = 1; i <= length; ++i) { val = amountCharacters*val; uniqueStrings = val }

此示例返回8,这是正确的.如果我尝试更高的值,例如amountCharacters = 10,它将返回1000.它仍然正确吗?

This example returns 8 which is correct. If I try with something higher, like amountCharacters = 10 it returns 1000. Is it still correct?

推荐答案

如果您有n个不同的字符,并且长度为k,则可以形成n 个可能的字符串.每个字符与其余字符无关,可以是n个不同选项之一,总共可以选择k个.您的代码正确.

If you have n different characters and the length is k, there are exactlty n 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.

对于2个可能的字符和10个字母,正好有1024个可能的字符串.

For 2 possible characters and 10 letters, there are exactly 1024 possible strings.

希望这会有所帮助!

这篇关于设定数量的字符和长度,可以有多少个唯一的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:17