【发布时间】:2021-11-11 09:14:03
【问题描述】:
我的练习包括对二进制字符串中的子序列进行计数,但我不能使用 count 方法,因为例如,如果字符串 '10010000' 中有子序列 '00',则 count 方法将返回 ' 3',但正确答案是'4'。
我试过了:
def subseq_uguale(stringa,testo,lunghezza):
f=0
for i in range(len(testo)-lunghezza+1):
confronta=''
for j in range(lunghezza):
confronta+=testo[i+j]
if confronta==stringa:
f+=1
return f
其中 'lunghezza' 是子序列的长度,'testo' 是序列,'stringa' 是子序列。但这需要太多时间! 我该如何解决这个问题?
【问题讨论】:
-
这是否回答了您的问题:stackoverflow.com/questions/2970520/…?
标签: python string count subsequence