区分大小写的字符串的组合计数
我记得,组合数等于 n!
但是,对于我来说,我有字符串“abc”。我想获得具有不同注册表的所有组合:aBc 或 ABc 等
所以,abc 是 3 个字符。 3! = 1 * 2 * 3 = 6。 但是,如果我尝试手动完成这项工作 - 我将得到 8 种变化:
1 abc 2 ABC 3 aBc 4 abC 5 ABC 6 BC 7 抗体 8 ABC
所以,看起来答案是 2^3 = 8,但是 2 是什么? 3 - 是字符串中的注册表计数。 2 是什么?注册表变体的数量?
As I remember, the count of combinations equal n!
But, for I example I have string "abc". I want to get all the combinations with different registry: aBc or ABc etc
So, abc is 3 chars. 3! = 1 * 2 * 3 = 6.
But, if I shall try manually do this work - I'll get 8 variations:
1 abc
2 Abc
3 aBc
4 abC
5 ABc
6 aBC
7 AbC
8 ABC
So, it looks like, that answer it 2^3 = 8, but what is 2 ? 3 - is the count of registries in string. what is 2 ? count of registry variants?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您想知道对于固定字符串,以混合大写形式编写固定字符串有多少种可能的组合。您对源字符串的实际排列不感兴趣,即您不想考虑对于 abc,还有 acb、cab、cba 等。是吗?
如果是这样,对于 1 个字母,我们有
两个字母
和三个字母,
依此类推。如果是这种情况,那么只要选择正确的底层模型,解决方案就非常简单。您可能已经注意到,结果与我们选择的字符序列无关 - 那么为什么不选择所有
a
:模式是对于每个字符,我们有两种可用选择,大写或小写,设置或不设置... 1 或 0 - 只需将 a 替换为 0,将 A 替换为 1 即可得到:
这实际上是二进制计数!因此,对于 n 个字母,可能的组合数量将等于 2^n。
If I understand correctly, you want to know for a fixed string how many possible combinations there are with respect to writing the fixed string in mixed capitalization. You are not interested in real permutations of the source string, i.e. you don't wan't to take into account that for abc there is also acb, cab, cba etc. Yes?
If so, for 1 letter we have
for two letters
and for three letters
and so on. If that's the case, then the solution is quite simple if you choose the right underlying model. As you may have noticed, the outcome is regardless of the character sequence we choose - so why not choose all
a
's:The pattern is that for each character we have two choices available, either uppercase or lowercase, either set or not set... either 1 or 0 - simply replace a with 0 and A with 1 to get:
That is actually binary counting! So for n letters the number of possible combinations will be equal to 2^n.
啊,我想我明白你在说什么。如果我理解正确,您正在寻找将字符串中的字母大写的所有可能方法,以使所有字母的大小写不同 - 也就是说,给定 abc,您会产生
但不是
因为所有字母在那些版本中有相同的情况。
如果这是您想要的,则可以在长度为 n 的非空字符串中执行此操作的方法数量为 2n - 2。直观地说,其背后的基本原理如下。给定一个包含 n 个字母的字符串,有 2n 种不同的方法来将该字符串中的所有字母大写,因为对于独立于其余字符的每个字符,该字母可以处于两种状态之一(上大写或小写)。如果考虑所有这些组合,您会发现有两种组合是您想要禁止的 - 所有字母均为大写的版本和所有字母均为小写的版本。
在您的问题中,您提到 n 元素序列的组合数为 n!。这不太正确。有n个! n 元素序列的排列(假设每个元素都是不同的)。比如有3个! = 序列 abc 的 6 种排列:
事实上,有六种方法可以将三个字母的序列大写,而不需要使所有字母具有相同的大写形式,并且 abc 有六种排列,这完全是巧合。如果您查看该系列的更多术语,您可以看到它们仅在两个位置(2 和 3)匹配:
如果您允许更多情况而不仅仅是上限和下限(例如,k 个不同版本),那么您可以概括这一点得到值 kn - k,因为有 kn 个不同的组合,其中 k 个都具有相同的大小写。
希望这有帮助!
Ah, I think I see what you're saying. If I understand you correctly, you are looking to find all possible ways of capitalizing the letters in a string such that all of the letters aren't of the same case - that is, given abc, you'd produce
But not
Because all letters in those versions have the same case.
If this is what you'd like, the number of ways you can do this in a nonempty string of length n is given by 2n - 2. Intuitively, the rationale behind this is as follows. Given a string of n letters, there are 2n different ways to capitalize all of the letters in that string, since for each character independently of the rest, that letter can be in one of two states (upper case or lower case). If you consider all of those combinations, there are exactly two that you want to disallow - the version where all letters are upper-case, and the version where all letters are lower-case.
In your question, you mentioned that the number of combinations of an n-element sequence is n!. This is not quite right. There are n! permutations of an n-element sequence (assuming each element is distinct). For example, there are 3! = 6 permutations of the sequence abc:
The fact that there are six ways to capitalize a three-letter sequence without giving all letters the same capitalization and that there are six permutations of abc is a complete coincidence. If you look at more terms of the series, you can see that they only match at two locations (2 and 3):
If you allow for more cases than just upper and lower (say, k different versions), then you can generalize this to get the value kn - k, since there are kn different combinations, of which k of them will all have the same capitalization.
Hope this helps!