给定二进制字符串计数所有二进制数的频率与各种lenghts
给定一个字符串,例如'0100011'
length = 2
我们必须计算字符串对的频率:00,01,10,11
。
00 - > 2,01-> 2,10 - > 1,11-> 1
问题是,如果我只使用count('00')
函数,则它不起作用,因为它只找到一个出现:<代码>“ _ _ _ 000 _ _” 。
我一直想知道解决此类问题的正确方法是什么,因为目前我尝试使用2个指针(索引和长度)创建某种形式的子字符串检查器,这些检查器来回肯定它不是错过任何组合,但感觉不对劲
Given a string like '0100011'
and a length = 2
we have to count the frequency of pairs in the string: 00,01,10,11
.
00 -> 2 , 01 -> 2 , 10 ->1 , 11->1
The problem is if I just use the count('00')
function, it doesn't work since it only finds one occurrence: " _ _ 000 _ _"
.
I've been wondering what the right approach to this kind of problem would be, since for the moment i've tried to create some sort of substring checker with 2 pointer(index and length) which goes back and forth to be sure it doesn't miss any combination but it doesn't feel right
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的做法:
将产生:
长度== 3:
...
对于长度== 7,显然:
here is how I did it:
will produce:
for length == 3:
...
for length == 7, obviously:
如果要找到单个二进制子字符串的出现数量,则可以使用字符串切片来获取每个连续的字母对:
此输出:
如果您想找到特定长度的所有二进制字符串的出现数量,则可以使用
collections.counter
而不是:输出:
此 返回
0
。)If you want to find the number of occurrences of a single binary substring, you can use string slicing to get each consecutive pair of letters:
This outputs:
If you want to find the number of occurrences of all binary strings of a particular length, you could use a
collections.Counter
instead:This outputs:
(Note that if you try to look up the frequency of a binary string that doesn't occur, the
Counter
will simply return0
.)