iterative lcs solution