Solution to LCS problem