Home > Software engineering >  Python. Count the number of subsequences of ABA
Python. Count the number of subsequences of ABA

Time:02-06

I have the string s = 'ABAABABBBBB'. It is necessary to count the number of subsequences of ABA. m is the maximum length of the sequence ABA. In this case, m = 2. Why does my program write that m=1?

s = 'ABAABABBBBB'
print (s)
k=0
m = 0
for i in range(2,len(s)):
    if s[i-2] == 'A' and s[i-1]=='B' and s[i]=='A':
        k=k 1
        m = max(k,m)
        i  = 2
    else:
        k=0
print(m)

CodePudding user response:

You could do sth like this

s = 'ABAABABBBBB'
m =0
for i in range(0,len(s)):
    if s[i:i 3] == "ABA":
        m =1
print(m)

CodePudding user response:

s = 'ABAABAABABBBBBBBBBBABAABAABAABAABA'
k = 0
m = 0
i = 2
print(len(s))
while (i!=len(s)):
    if s[i-2] == 'A' and s[i-1]=='B' and s[i]=='A':
        k =1
        m=max(k,m)
        i  = 3
        if (i>len(s)):
            break
    else:
        k=0
        i =1
print(m)
  • Related