• + 0 comments

    Can anyone help me in this?

    M=62
    def func(c):
        if(c>='a' and c<='z'):
            return ord(c)-97
        if(c>='A' and c<='Z'):
            return ord(c)-65
        return ord(c)-48
    
    def tutzkiAndLcs(a, b):
        n=len(a)
        m=len(b)
        dp=[]
        dpr=[]
        position=[[] for i in range(M)]
        for i in range(1,m+1,1):
            position[func(b[i-1])].append(i)
        for i in range(n+2):
            dp+=[[0]*(m+2)]
            dpr+=[[0]*(m+2)]
        
        for i in range(1,n+1):
            for j in range(1,m+1):
                if a[i-1]==b[j-1]:
                    dp[i][j]=dp[i-1][j-1]+1
                else:
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1])
    
                
        for i in range(n,0,-1):
            for j in range(m,0,-1):
                if(a[i-1]==b[j-1]):
                    dpr[i][j]=1+dpr[i+1][j+1]
                else:
                    dpr[i][j]=max(dpr[i+1][j],dpr[i][j+1])
                    
        #print(position)
        #print(dp)
        #print(dpr)   
        
        ans = 0
        for i in range(0,n+1,1):
            for c in range(0,26,1):
                for j in range(0,len(position[c]),1):
                    p=position[c][j]
                    if(dp[i][p-1] + dpr[i+1][p+1] == dp[n][m]):
                        ans+=1
                        break
        print(ans)    
        return ans