My exercise consists in counting a subsequence in a binary string, but I can't use the count method because, for example, if I have the subsequence '00' in the string '10010000', the count method will return '3' but the correct answer is '4'.
I tried this:
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
Where 'lunghezza' is the length of the subsequence, 'testo' is the sequence and 'stringa' is the subsequence. But it takes too much time! How can I solve this?
Try this
def subseq_uguale(stringa, testo):
f=0
for i in range(len(testo)+1-len(stringa)):
if stringa==testo[i:i+len(stringa)]:
f+=1
return f