T='bbeeae' m=len(T) res=1 dp=[[0]*(m+1) for _ in range(m+1)] for i in range(m,0,-1) : c=False d=False e=False f=False for j in range(i,m+1): if T[j-1]=='e': dp[i][j] = dp[i][j-1]+1 e=True if T[j-1]=='f': dp[i][j] = dp[i][j-1]+1 ...