Longest common subsequence solution reconstruction during DP is wrong I've implemented longest common substring in python. I know the usual way to reconstruct the path. But, I am reading my algorithm many times, I don't understand why it doesn't work for some test-cases. I am trying to construct the solution while the longest path is being made through dynamic programming.
def lcs(s1, s2):
    n = len(s1)
    m = len(s2)
    
    trow = [0] * (m+1)
    table = [trow] * (n+1)
    
    srow = [&quot;&quot;] * (m+1)
    solution = [srow] * (n+1)
    
    for i in range(1, n+1):
        for j in range(1, m+1):
            if s1[i-1] == s2[j-1]:                
                table[i][j] = table[i-1][j-1] + 1
                
                # Adding this character to the solution
                solution[i][j] = solution[i-1][j-1] + s1[i-1]
            
            elif table[i][j-1] &gt; table[i-1][j]:
                table[i][j] = table[i][j-1]
                
                # Choosing the solution related to the longer path
                solution[i][j] = solution[i][j-1] 
                
            else:
                table[i][j] = table[i-1][j]
                
                # Choosing the solution related to the longer path
                solution[i][j] = solution[i-1][j] 
    
    return solution[n-1][m-1]

lcs('amin', 'kahmjiknln')
# Returns 'amin'

lcs('amin', 'aaaakahmjiknln')
# Returns 'aaaaamin'
Programming questions are off-topic here.
Anyway your matrices table and solution are not properly initialized as they contain references to the same objects. Try:
    table = [[0]*(m+1) for j in range(n+1)]
    solution = [ [&quot;&quot; for i in range(m+1)] for j in range(n+1) ]

Moreover, the returned string should be solution[n][m] and not solution[n-1][m-1].